MySQL Datetime to PHP date format

By shane
Mon, 2012-07-30 16:39
comments

Share with Others

An easy one today. I often have to remind myself of how to convert a MySQL datetime to something a little more usable... a UNIX timestamp.

The strototime version

It is incredibly easy, just use the PHP strtotime function.

$timestamp = strtotime($result->my_datetime);

Now you can print the date in any format you want:

print date("Y-m-d H:i:s", $timestamp);

PHP5 Convert MySQL Datetime to PHP date format

If you are using PHP5, the following should accomplish the same thing.

$dtime = new DateTime($result->my_datetime);
print $dtime->format("Y-m-d H:i:s");

Sometimes we all need a reminder of the simple things.