PHP 文件修改时间使用 GMT 偏移设置来报告正确时间

发布于 2024-11-14 12:22:31 字数 506 浏览 4 评论 0原文

我目前正在报告文件修改时间,如下所示:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箹锭⒈辈孓 2024-11-21 12:22:31

filemtime() 将返回基于服务器的时钟。由于您有用户到 gmt 的偏移可用,因此您必须将 unix 时间戳转换为 GMT,然后转换为用户的 timszone,如下所示:

<?php
    list($temp_hh, $temp_mm) = explode(':', date('P'));
    $gmt_offset_server = $temp_hh + $temp_mm / 60;
    $gmt_offset_user   = -7.0;
    $timestamp         = filemtime(__FILE__);
    echo sprintf('
        Time based on server time.........: %s
        Time converted to GMT.............: %s
        Time converted to user timezone...: %s
        Auto calculated server timezone...: %s
        ',
        date('Y-m-d h:i:s A', $timestamp),
        date('Y-m-d h:i:s A', $timestamp - $gmt_offset_server * 3600),
        date('Y-m-d h:i:s A', $timestamp - $gmt_offset_server * 3600 + $gmt_offset_user * 3600),
        $gmt_offset_server
    );

    // Output based on server timezone = PKT (+05:00 GMT) and user timezone = PDT (-07:00 GMT)
    // Time based on server time.........: 2011-06-09 03:54:38 PM
    // Time converted to GMT.............: 2011-06-09 10:54:38 AM
    // Time converted to user timezone...: 2011-06-09 03:54:38 AM
    // Auto calculated server timezone...: 5

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:

<?php
    list($temp_hh, $temp_mm) = explode(':', date('P'));
    $gmt_offset_server = $temp_hh + $temp_mm / 60;
    $gmt_offset_user   = -7.0;
    $timestamp         = filemtime(__FILE__);
    echo sprintf('
        Time based on server time.........: %s
        Time converted to GMT.............: %s
        Time converted to user timezone...: %s
        Auto calculated server timezone...: %s
        ',
        date('Y-m-d h:i:s A', $timestamp),
        date('Y-m-d h:i:s A', $timestamp - $gmt_offset_server * 3600),
        date('Y-m-d h:i:s A', $timestamp - $gmt_offset_server * 3600 + $gmt_offset_user * 3600),
        $gmt_offset_server
    );

    // Output based on server timezone = PKT (+05:00 GMT) and user timezone = PDT (-07:00 GMT)
    // Time based on server time.........: 2011-06-09 03:54:38 PM
    // Time converted to GMT.............: 2011-06-09 10:54:38 AM
    // Time converted to user timezone...: 2011-06-09 03:54:38 AM
    // Auto calculated server timezone...: 5
×纯※雪 2024-11-21 12:22:31

您需要的是 strtotime() 函数。将日期更改为 gmdate,将服务器时间转换为 GMT

例如,如果您需要 10:00:00 等时间格式,请

gmdate("H:i:s", strtotime($gmt_offset . " hours"));

在此处了解更多信息:

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 GMT

For example if you need the time format like 10:00:00

gmdate("H:i:s", strtotime($gmt_offset . " hours"));

More info here:

http://php.net/manual/en/function.strtotime.php

http://php.net/manual/en/function.gmdate.php

只等公子 2024-11-21 12:22:31
$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"] + ($gmt_offset * 3600));

$gmt_offset 应该是 float 类型,而不是 int 类型——某些时区可能存在小数差异,例如阿德莱德的 GMT +09:30

$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"] + ($gmt_offset * 3600));

$gmt_offset should be of type float, not int -- some time zones can have fractional difference, like GMT +09:30 for Adelaide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文