使用 php 格式化 mysql 时间戳

2022-08-30 15:29:05

我有名为时间戳的列。当我直接打印它时:postDate

echo $result['postDate']; 

另一方面,当我通过日期函数打印它时,我确实得到了存储的内容(例如):2011-03-16 16:48:24

echo date('F/j/Y',$result['postDate'])

我得到December/31/1969

我做错了什么?

非常感谢


答案 1

试试这个。

date('F/j/Y',strtotime($result['postDate']));

因为时间戳是必需的,而不是将格式化的日期作为第二个参数。或者您也可以尝试

SELECT UNIX_TIMESTAMP(postDate) as postDateInt from myTable

而不是,然后在代码中包含它。SELECT postDate from myTable

date('F/j/Y',$result['postDateInt']);

答案 2

PHP 日期函数查找 a 作为第二个参数。尝试使用 strtotime()int time()

echo date('F/j/Y', strtotime($result['postDate']) );

推荐