Format date from database?


Question

When I pull the date out of the db, it comes back like this:

2009-10-14T19:00:00

I want to format it in two different ways...

The first: F d, Y The second h:m (12 hour format)

Everything I try returns December 1969... Help?! I feel so confused...

1
22
2/12/2013 4:42:11 PM

Accepted Answer

Normally the code is just:

echo date('F d, Y h:mA', strtotime('2009-10-14 19:00:00'));

Note that if strtotime() can't figure out the date, it returns the time as 1/1/1970 00:00:00 GMT.

55
10/8/2009 2:16:25 AM

If you want to format it in the database (assuming MySQL):

SELECT DATE_FORMAT(t.column, '%M %D, %Y'), --October 14, 2009
       DATE_FORMAT(t.column, '%h:%i %p') --hh:mm am/pm
  FROM TABLE t

...or if you want to do the conversion in PHP:

echo date('F d, Y h:mA', strtotime('2009-10-14 19:00:00'));

Reference:


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon