首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP strtotime没有用mysql值执行类似的转换

PHP strtotime没有用mysql值执行类似的转换
EN

Stack Overflow用户
提问于 2018-08-28 04:50:50
回答 1查看 0关注 0票数 0

所以我从一个mysql数据库中提取时间戳,并想知道为什么没有时间检查工作。

代码语言:javascript
复制
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>";

输出:

代码语言:javascript
复制
时间是2018-08-18 12:42:55
Mysql时间戳2018-08-18 12:42:52
Mysql时间戳为strtotime1534596172
strtotime当前时间1534552975

转换为字符串后,数字之间大约有4000个差异。发生了什么,如何在不做的情况下解决这个问题x-4000

EN

回答 1

Stack Overflow用户

发布于 2018-08-28 14:44:21

区别在于您当地的时区。date()使用您的本地时区计算时间,而strtotime("now")time()使用UTC时间。比较:

代码语言:javascript
复制
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):

代码语言:javascript
复制
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

代码语言:javascript
复制
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>";

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100008822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档