I'm reading a datetime field from mysql db. I'd like to convert it in PHP:
from: 2009-05-23 14:46:23
to 05/23/2009 02:46 pm
Notice the am/pm
conversion.
Thanks in advance..
I'm reading a datetime field from mysql db. I'd like to convert it in PHP:
from: 2009-05-23 14:46:23
to 05/23/2009 02:46 pm
Notice the am/pm
conversion.
Thanks in advance..
// assume you retrieve the mysql date in variable $date
date("m/d/Y h:i a", strtotime($date));
Call me old fashioned. I store all dates as epoch, and if I will ever go outside my timezone, as epoch of UTC of the date.
Epoch is seconds since Jan 1st, 1970 or something like that.
Known in MySQL as Unixtime. FROM_UNIXTIME(), etc..
$epoch = strtotime($mdate);
You can also store dates directly in MySQL with data type DATETIME if you are so inclined. Your chosen format is a drop in replacement actually.
Depending on the type that's return by your db code, you can create a new DateTime object or format an existing one with date_format()
. From the example you give, the date format string should be "m/d/Y h:i a"
. Note that the conversion from 24h to 12h is handled in either case.
Have a look at the format options for DateTime:date()
and DateTime:format()
(date_format is an alias for this)