简体   繁体   中英

Format date from database?

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...

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 .

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:

简单的日期显示功能:

 echo date('m-d-Y H:i:s',strtotime($date_variable));

Best of all and clear

$res_tbl ='select create_date from tbl_comments';
while($table =mysql_fetch_array($res_tbl))
{
  echo date('F d, Y h:mA', strtotime($table['create_date']));
  echo "<br>";
}

Output:

January 10, 2009 21:12
March 21, 2001  12:04

You might wanna check strtotime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM