使用 DateTime/DateTimeZone 在 PHP 中调整时区

2022-01-16 00:00:00 datetime timezone php

有很多关于在 PHP 中进行时区调整的信息,但由于所有的噪音,我还没有找到具体我想要做什么的答案.

There's a lot of info on doing time zone adjustments in PHP, but I haven't found an answer for specifically what I want to do due to all the noise.

给定一个时区的时间,我想将其转换为另一个时区的时间.

Given a time in one timezone, I want to convert it to the time in another timezone.

这个本质上就是我想做的,但我需要能够仅使用内置的 PHP 库而不是 PEAR Date 来做到这一点.

This is essentially what I want to do, but I need to be able to do it using only the built-in PHP libs, not PEAR Date.

这是我一直在做的,但它似乎总是给我相对于 GMT 的偏移量:

This is what I've been doing, but it seems to always give me the offset relative to GMT:

$los_angeles_time_zone = new DateTimeZone('America/Los_Angeles');
$hawaii_time_zone = new DateTimeZone('Pacific/Honolulu');

$date_time_los_angeles = new DateTime('2009-09-18 05:00:00', $los_angeles_time_zone);
printf("LA Time: %s<br/>", $date_time_los_angeles->format(DATE_ATOM));

$time_offset = $hawaii_time_zone->getOffset($date_time_los_angeles);
printf("Offset: %s<br/>", $time_offset);

这是输出:

洛杉矶时间:2009-09-18T05:00:00-07:00
偏移量:-36000

LA Time: 2009-09-18T05:00:00-07:00
Offset: -36000

我期待 3 小时(10800 秒).但是 '-7:00' 告诉我它保留了相对于 GMT 的所有内容,这也许可以解释为什么它给了我绝对"偏移量.

I was expecting 3 hours (10800 seconds). but the '-7:00' thing tells me it's keeping everything relative to GMT, which maybe explains why it's giving me the "absolute" offset.

如何在没有所有这些 GMT 的情况下获得两个时区之间的偏移量?

How do I just get the offset between the two timezones without all of this GMT hoohah?

谢谢.

更新:

我突然想到我可以这样做并得到我想要的:

I occured to me that I could do this and get what I want:

    $date_time_los_angeles = new DateTime('2009-09-18 05:00:00', $los_angeles_time_zone);
printf("LA Time: %s<br/>", $date_time_los_angeles->format(DATE_ATOM));

$date_time_hawaii = new DateTime('2009-09-18 05:00:00', $hawaii_time_zone);
printf("Hawaii Time: %s<br/>", $date_time_hawaii->format(DATE_ATOM));


$time_offset = $los_angeles_time_zone->getOffset($date_time_los_angeles) - $hawaii_time_zone->getOffset($date_time_los_angeles);
printf("Offset: %s<br/>", $time_offset);

但这对我来说感觉很尴尬.有人知道更清洁的方法吗?

But it feels awkward to me. Anyone know a cleaner way to do it?

推荐答案

这里有几个使用 DateTime 类的函数.第一个将返回两个时区之间的秒差.第二个返回时间从一个时区到另一个时区的转换".

Here are a couple of functions using the DateTime classes. The first one will return the difference in seconds between two timezones. The second returns a "translation" of the time from one timezone to another.

function timezone_diff($tz_from, $tz_to, $time_str = 'now')
{
    $dt = new DateTime($time_str, new DateTimeZone($tz_from));
    $offset_from = $dt->getOffset();
    $timestamp = $dt->getTimestamp();
    $offset_to = $dt->setTimezone(new DateTimezone($tz_to))->setTimestamp($timestamp)->getOffset();
    return $offset_to - $offset_from;
}

function time_translate($tz_from, $tz_to, $time_str = 'now', $format = 'Y-m-d H:i:s')
{
    $dt = new DateTime($time_str, new DateTimezone($tz_from));
    $timestamp = $dt->getTimestamp();
    return $dt->setTimezone(new DateTimezone($tz_to))->setTimestamp($timestamp)->format($format);
}

演示:

$los_angeles_time = '2009-09-18 05:00:00';
$los_angeles_tz   = 'America/Los_Angeles';
$hawaii_tz        = 'Pacific/Honolulu';

$los_angeles_hawaii_diff = timezone_diff($los_angeles_tz, $hawaii_tz, $los_angeles_time);
echo $los_angeles_hawaii_diff . '<br />';

$hawaii_time = time_translate($los_angeles_tz, $hawaii_tz, $los_angeles_time);
echo $hawaii_time . '<br />';

相关文章