View Full Version : PHP Array?

10-23-2012, 12:58 AM
I am trying to combine several variables into one PHP array (I think)

Basically the user keys in the date and time into 6 boxes:


I am then trying to combine those fields into one array.

Whatever I'm doing is not working and I'd really appreciate some help.

$start_time = $start_year,$start_month,$start_date,$start_hour,$start_minute,$start_seconds;
echo $start_time;

10-23-2012, 01:11 AM
Looks to me that you mean a string, not an array.
To convert that to an array, you'd simply wrap it in array(). So that would be array($start_year, . . ., $start_seconds);.

What you want is a string. I'd recommend a sprintf as its simple.

$start_time = sprintf('%d %d %d %d %d %d', $start_year, $start_month, $start_date, $start_hour, $start_minute, $start_seconds);

Since these come from HTML, you can also generate the array there. That may be a lot easier to work with and be more beneficial overall.

<input type="text" name="inputDate[year]" />
<input type="text" name="inputDate[month]" />

Then it will be retrieved as an array already. This is nice as to match the format you have above you'd simply implode() with a space, but you can still look up what you need based on the offset of $_POST['inputDate']['itemhere'].

BTW, if you split up the date from the time strings, you can parse it easily using strtotime or new DateTime objects. This makes it easier to deal with other calculations based on time as well as letting you format it however you want.

// input types date and time
$sDateStr = implode('', $_POST['inputDate']);
$sTimeStr = implode('.', $_POST['inputTime']);
$dt = new DateTime("$sDateStr $sTimeStr");
print $dt->format('Y-m-d H:i:s');

For example. strtotime() and date() can be used in place of datetime objects.