PHP 文件修改时间使用 GMT 偏移设置来报告正确时间
我目前正在报告文件修改时间,如下所示:
$this->newScanData[$key]["modified"] = filemtime($path."/".$file);
$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"]);
对我来说,我认为这没有任何问题,但我的代码的用户报告时间为 4 小时。我能想到这一点的唯一原因是服务器与用户位于不同的时区。每个用户都有一个变量,我可以使用 $gmt_offset
来存储用户所在的时区。$gmt_offset
存储为基本浮点偏移量。
服务器可以位于任何时区,不一定位于 GMT-0。服务器可能与用户不在同一时区。
如何让 $modifiedtime
根据 $gmt_offset
获得用户所在时区的正确时间?
I'm currently reporting file modified time like so:
$this->newScanData[$key]["modified"] = filemtime($path."/".$file);
$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"]);
To me I thought there was nothing wrong with that but a user of my code is reporting the time being 4 hours out. The only reason why I can think of this is because the server is in a different timezone to the user. Each user has a variable I can use $gmt_offset
that stores the time zone that user is in. $gmt_offset
is stored as a basic float offset.
The server could be in any timezone, not necessarily in GMT-0. The server might not be in the same timezone as the user.
How do I get $modifiedtime
to have the correct time for the user in his timezone based on $gmt_offset
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
filemtime()
将返回基于服务器的时钟。由于您有用户到 gmt 的偏移可用,因此您必须将 unix 时间戳转换为 GMT,然后转换为用户的 timszone,如下所示:filemtime()
will return a unix timestamp based on the server's clock. Since you have user to gmt offset available, you must convert the unix timestamp to GMT and then into user's timszone as follows:您需要的是
strtotime()
函数。将日期更改为 gmdate,将服务器时间转换为 GMT例如,如果您需要 10:00:00 等时间格式,请
在此处了解更多信息:
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.gmdate.php
What you need is the
strtotime()
function. Changed date to gmdate, converting your servers time to GMTFor example if you need the time format like 10:00:00
More info here:
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.gmdate.php
$gmt_offset 应该是 float 类型,而不是 int 类型——某些时区可能存在小数差异,例如阿德莱德的 GMT +09:30
$gmt_offset should be of type float, not int -- some time zones can have fractional difference, like GMT +09:30 for Adelaide