我在MySQL中有一个日期时间列。
如何将其转换为显示为mm/dd/yy H:M (AM/PM)使用PHP?
我在MySQL中有一个日期时间列。
如何将其转换为显示为mm/dd/yy H:M (AM/PM)使用PHP?
当前回答
要在PHP中正确格式化DateTime对象以存储在MySQL中,请使用MySQL使用的标准化格式,即ISO 8601。
PHP从5.1.1版本开始就将这种格式存储为常量,我强烈建议使用这种格式,而不是每次都手动输入字符串。
$dtNow = new DateTime();
$mysqlDateTime = $dtNow->format(DateTime::ISO8601);
这个和其他PHP DateTime常量的列表可以在http://php.net/manual/en/class.datetime.php#datetime.constants.types上找到
其他回答
使用PHP版本4.4.9和MySQL 5.0,这对我来说是有效的:
$oDate = strtotime($row['PubDate']);
$sDate = date("m/d/y",$oDate);
echo $sDate
PubDate是MySQL中的列。
$valid_date = date( 'm/d/y g:i A', strtotime($date));
参考:http://php.net/manual/en/function.date.php
这会有用的……
echo date('m/d/y H:i (A)',strtotime($data_from_mysql));
直接输出,如德文格式:
echo(date('d.m.Y H:i:s', strtotime($row["date_added"])));
要在PHP中正确格式化DateTime对象以存储在MySQL中,请使用MySQL使用的标准化格式,即ISO 8601。
PHP从5.1.1版本开始就将这种格式存储为常量,我强烈建议使用这种格式,而不是每次都手动输入字符串。
$dtNow = new DateTime();
$mysqlDateTime = $dtNow->format(DateTime::ISO8601);
这个和其他PHP DateTime常量的列表可以在http://php.net/manual/en/class.datetime.php#datetime.constants.types上找到