在 PHP 中处理时区和日期

发布于 2024-10-31 05:22:20 字数 629 浏览 1 评论 0原文

我正在美国的服务器上运行一项托管服务,该服务读取使用本地日期创建的 XML 提要(目前仅限英国),但我想确保该服务适用于所有时区。 我的流程查看提要中帖子的日期,并将其与当前的日期/时间(在美国的服务器上)进行比较。 我提出的解决方案将系统本地化为提要的发起者,然后创建一个时间戳来与“现在”进行比较:

protected function datemath($thedate){

    $currenttimezone = date_default_timezone_get();
    date_default_timezone_set($this->feedtimezone);
    $thedate = mktime substr($thedate,11,2),substr($thedate,14,2),
    substr($thedate,17,2),substr($thedate,3,2),substr($thedate,0,2),
    substr($thedate,6,4));
    date_default_timezone_set($currenttimezone);
    return $thedate;

    }

我的问题是......这是处理此问题的合理方法还是有更好的方法,我真正应该知道的更标准化的方式?

I am running a service hosted on a server in the US which reads an XML feed that has been created with a local date - currently just the UK, but I want to ensure the service works with all timezones.
My process looks at the date of a post in a feed and compares it with the date/time right now(on the server in the US).
The solution I came up with localises the system to the originator of the feed and then creates a timestamp with which to compare 'now' with:

protected function datemath($thedate){

    $currenttimezone = date_default_timezone_get();
    date_default_timezone_set($this->feedtimezone);
    $thedate = mktime substr($thedate,11,2),substr($thedate,14,2),
    substr($thedate,17,2),substr($thedate,3,2),substr($thedate,0,2),
    substr($thedate,6,4));
    date_default_timezone_set($currenttimezone);
    return $thedate;

    }

My question is this... Is this a reasonable way of handling this issue or is there a better, more standardized way that I really should know?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

烂柯人 2024-11-07 05:22:20

这是我编写的用于进行时区转换的函数。应该是不言自明的:

function switch_timezone($format, $time = null, 
    $to = "America/Los_Angeles", $from = "America/Los_Angeles")
{
    if ($time == null) $time = time();

    $from_tz = new DateTimeZone($from);
    $to_tz = new DateTimeZone($to);

    if (is_int($time)) $time = '@' . $time;

    $dt = date_create($time, $from_tz);

    if ($dt)
    {
        $dt->setTimezone($to_tz);
        return $dt->format($format);
    }

    return date($format, $time);
}

Here's a function I wrote to do timezone conversions. Should be pretty self-explanatory:

function switch_timezone($format, $time = null, 
    $to = "America/Los_Angeles", $from = "America/Los_Angeles")
{
    if ($time == null) $time = time();

    $from_tz = new DateTimeZone($from);
    $to_tz = new DateTimeZone($to);

    if (is_int($time)) $time = '@' . $time;

    $dt = date_create($time, $from_tz);

    if ($dt)
    {
        $dt->setTimezone($to_tz);
        return $dt->format($format);
    }

    return date($format, $time);
}
瑕疵 2024-11-07 05:22:20

经过更多检查其他人的代码后,我发现该函数

strtotime($thedate);

比使用 mktime 更简洁,并且还允许不同的时间格式。

After a bit more checking of other peoples code I see the function

strtotime($thedate);

is a little bit more succinct than using mktime and also allows for different time formats.

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