php 从数据库格式化日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1535246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format date from database?
提问by n00b0101
When I pull the date out of the db, it comes back like this:
当我从数据库中取出日期时,它会像这样返回:
2009-10-14T19:00:00
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)
第一个:F d, Y 第二个 h:m(12 小时格式)
Everything I try returns December 1969... Help?! I feel so confused...
我尝试的一切都返回了 1969 年 12 月......帮助?!我觉得很迷茫...
回答by too much php
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.
请注意,如果strtotime()无法确定日期,它会将时间返回为1/1/1970 00:00:00 GMT。
回答by Gaurav Gupta
Simple date display function :
简单的日期显示功能:
echo date('m-d-Y H:i:s',strtotime($date_variable));
回答by OMG Ponies
If you want to format it in the database (assuming MySQL):
如果要在数据库中对其进行格式化(假设为 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:
...或者如果你想在 PHP 中进行转换:
echo date('F d, Y h:mA', strtotime('2009-10-14 19:00:00'));
Reference:
参考:
回答by Shahid Karimi
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

