为了账号安全,请及时绑定邮箱和手机立即绑定

PHP time()是否返回GMT / UTC时间戳?

PHP time()是否返回GMT / UTC时间戳?

PHP
largeQ 2019-12-13 15:10:49
我只想检查是否time()返回UTC / GMT时间戳,还是需要使用date_default_timezone_set()?
查看完整描述

3 回答

?
慕容708150

TA贡献1831条经验 获得超4个赞

从PHP 5.1.0(重写日期/时间函数时)开始,如果时区无效,则每次对日期/时间函数的调用都会生成E_NOTICE,如果使用系统设置或TZ,则每次生成E_WARNING消息环境变量。


因此,为了获得UTC时间戳,您应该检查当前时区是什么,并解决该时区或仅使用:


$utc_str = gmdate("M d Y H:i:s", time());

$utc = strtotime($utc_str);



查看完整回答
反对 回复 2019-12-14
?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

声明之一:“ time()和strtotime(gmdate(“ M d YH:i:s”,time()))均返回相同结果”


由于我不确定,因此进行了测试:


$now = strtotime(gmdate("Y-m-d H:i:s", time()));

$now2 = time();

echo ' now='.$now.' now2='.$now2.' diff='.($now - $now2);

输出为:


 now=1536824036 now2=1536806036 diff=18000

差异为18000秒= 5小时=运行测试的服务器的时区偏移量。



查看完整回答
反对 回复 2019-12-14
  • 3 回答
  • 0 关注
  • 290 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信