views:

64

answers:

6

I have a datetime column in MySQL let's call it $time. It's coming form a CakePHP form. If I try to echo it I get I just get "Array". If I print_r() on it I get:

Array ( [month] => 10 [day] => 30 [year] => 2010 [hour] => 16 [min] => 30 )

I want to echo this out as a formatted date, nothing seems to work because it's not a string but an array. Do I have to go like this:

echo $time['month'].'-'.$time['day'].'-'.$time['year'].' '.$time['hour'].':'.$time['min'];

or can I use the date function?

+8  A: 

One simple, procedural way is to use date() in conjunction with mktime(), like so. date() formats based on a UNIX timestamp; mktime() provides a timestamp based on your array values:

$timestamp = mktime($time['hour'], $time['min'], 0, $time['month'], $time['day'], $time['year']);
echo date('M-d-Y H:i', $timestamp);

For a more object-oriented approach with the DateTime class, refer to Gordon's answer.

BoltClock
thanks for your help! Thanks to everyone else too, this one worked right away though.
pg
+2  A: 

Do I have to go like this:

That is one option, yes.

or can I use the date function?

Not with the data in the current form.

You could consider converting the array into a proper timestamp or DateTime object for maximum flexibility in formatting, calculations etc.

  • For a timestamp, see mktime() (You'll have to feed it the members of your array. Update: @BoltClock has an example.)

  • For a DateTime object - it's PHP5's new, object-oriented, Year 2038 bug-free, and much better way of dealing with dates - see CreateFromFormat() (Needs PHP 5.3+, though)

Pekka
+1  A: 

The date() function has an optional argument $timestamp. You can echo date("MM-dd-yyyy hh:mm, $time) and avoid manual formatting.

I hope the $time value is declared as TIMESTAMP in MySQL for this to work

djechelon
+1  A: 

Date function wants a timestamp. But you could use a custom function, such as:

function fd($time) {
   return "$time[month]-$time[day]-$time[year] $time[hour]:$time[minute]";
}

// Sample usage
echo fd($time);
Saul
+5  A: 

You can do:

$dt = new DateTime;
$dt->setDate($time['year'], $time['month'], $time['day']);
$dt->setTime($time['hour'], $time['minute']);
echo $dt->format(DateTime::ISO8601);

You can put any format into format() that is also supported with date().
You do not need PHP5.3 for this.

Use the above if you need to create a date that is not already contained in the array. If you simply want a 'Y-m-d H:i' format, you can use

printf("%d-%02d-%02d %02d:%02d",
       $time['year'], $time['month'], $time['day'],
       $time['hour'], $time['min']);

or with argument swapping and passing the entire array (though you have to rely on the order then):

vprintf('%3$d-%1$02d-%2$02d %4$02d:%5$02d', $time);

Needless to say, you can also use vsprintf or sprintf to create a datetime string that can be parsed with DateTime or strtotime, e.g.

$dt = new DateTime(vsprintf('%3$d-%1$02d-%2$02d %4$02d:%5$02d', $time));

which you could then format as shown in the first example.

Gordon
I thought of `vprintf()` too, but as you say, ordering of elements then becomes a concern.
BoltClock