将时间戳转换为日期时间后返回 - 差异来自何处?

use*_*671 -2 php mysql datetime timestamp

因为使用时间戳可以在mysql中获得更好的性能,所以我希望将时间保存为数据库中的时间戳.我收到一个这样的日期时间字符串:2014-09-24 17:18:27,将其转换为时间戳,strtotime($the_date_from_above)只是为了好玩我将它转换回来date("Y-m-d H:m:s", strtotime($the_date_from_above))

我var_dumped过程:

string(19) "2014-09-24 17:18:27"
int(1411579107)
string(19) "2014-09-24 17:09:27"
Run Code Online (Sandbox Code Playgroud)

在字符串转换期间出了什么问题?是什么导致了这种差异?这是相同的秒数,但9分钟的差异是奇怪的.

Daa*_*aan 7

您需要将其转换回来:

date("Y-m-d H:i:s", strtotime($the_date_from_above))
Run Code Online (Sandbox Code Playgroud)

当你以你的方式转换它时,min H:m:s被视为月份.

更好的是使用datetime类:

$date = new DateTime();
$date = $date->setTimestamp($yourtimestamp);
echo $date->format("Y-m-d H:i:s");
Run Code Online (Sandbox Code Playgroud)