php strotime没有执行与mysql值类似的转换

pftdvrlh  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(373)

所以我从mysql数据库中提取了一个时间戳,我想知道为什么所有的时间检查都不起作用。

echo "The time is " . date("Y-m-d h:i:s");
echo "</br>";
echo "Mysql Timestamp " . $row["Uptime"];
echo "</br>";
echo "Mysql Timestamp as strtotime" .strtotime($row["Uptime"]);
echo "</br>";
echo "strtotime Current Time". strtotime("now");
echo "</br>";

输出:

The time is 2018-08-18 12:42:55
Mysql Timestamp 2018-08-18 12:42:52
Mysql Timestamp as strtotime1534596172
strtotime Current Time1534552975

转换为字符串后,数字之间大约有4000个差异。怎么回事?我怎么能不做就解决这个问题 x-4000 ?

j0pj023g

j0pj023g1#

这种差异是由于您当地的时区。 date() 使用本地时区计算时间,而 strtotime("now") 以及 time() 使用utc时间。比较:

echo "The time is " . date("Y-m-d h:i:s");
echo "</br>";
echo 'strtotime("now") is '. strtotime("now");
echo "</br>";
echo 'strtotime date("Y-m-d h:i:s") is '. strtotime(date("Y-m-d h:i:s"));
echo "</br>";
echo "time() is ". time();
echo "</br>";

输出(时区) Australia\Adelaide ):

The time is 2018-08-17 09:06:35
strtotime("now") is 1534554395
strtotime date("Y-m-d h:i:s") is 1534511195
time() is 1534554395

现在尝试将时区设置为 UTC :

date_default_timezone_set('UTC');
echo "The time is " . date("Y-m-d h:i:s");
echo "</br>";
echo 'strtotime("now") is '. strtotime("now");
echo "</br>";
echo 'strtotime date("Y-m-d h:i:s") is '. strtotime(date("Y-m-d h:i:s"));
echo "</br>";
echo "time() is ". time();
echo "</br>";

输出-如您所见,所有窗体都返回相同的值:

The time is 2018-08-18 01:07:21
strtotime("now") is 1534554441
strtotime date("Y-m-d h:i:s") is 1534554441
time() is 1534554441

相关问题