Convert from MySQL datetime to another format with

2018-12-31 05:11发布

I have a datetime column in MySQL.

How can I convert it to the display as mm/dd/yy H:M (AM/PM) using PHP?

17条回答
浪荡孟婆
2楼-- · 2018-12-31 05:32

Using PHP version 4.4.9 & MySQL 5.0, this worked for me:

$oDate = strtotime($row['PubDate']);
$sDate = date("m/d/y",$oDate);
echo $sDate

PubDate is the column in MySQL.

查看更多
低头抚发
3楼-- · 2018-12-31 05:33

This should format a field in an SQL query:

SELECT DATE_FORMAT( `fieldname` , '%d-%m-%Y' ) FROM tablename
查看更多
心情的温度
4楼-- · 2018-12-31 05:35

Forget all. Just use:

$date = date("Y-m-d H:i:s",strtotime(str_replace('/','-',$date)))
查看更多
有味是清欢
5楼-- · 2018-12-31 05:36

You can have trouble with dates not returned in Unix Timestamp, so this works for me...

return date("F j, Y g:i a", strtotime(substr($datestring, 0, 15)))
查看更多
不再属于我。
6楼-- · 2018-12-31 05:39

If you are using PHP 5, you can also try

$oDate = new DateTime($row->createdate);
$sDate = $oDate->format("Y-m-d H:i:s");
查看更多
倾城一夜雪
7楼-- · 2018-12-31 05:39

This will work...

echo date('m/d/y H:i (A)',strtotime($data_from_mysql));
查看更多
登录 后发表回答