1

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?

1
  • I presume that you are trying to do this outside of CakePHP? Commented Oct 28, 2010 at 20:54

6 Answers 6

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your help! Thanks to everyone else too, this one worked right away though.
5

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.

1 Comment

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

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)

Comments

1

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

Comments

1

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);

Comments

0

Does the Time helper not suit the needs here? Just pass the whole array to it and use the functions it provides.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.