且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP - 使用日期找出夏令时

更新时间:2023-11-29 12:42:04

您不能使用 strtotime ,因为它创建了一个UTC时间戳记,可以删除时区信息。相反,只需使用 DateTime

You can't use strtotime because it creates a UTC timestamp, which removes the timezone information. Instead, just use DateTime

$date = new DateTime('30-Mar-2013 America/Los_Angeles');
echo $date->format('I');
# 1

$date = new DateTime('31-Mar-2013 America/Los_Angeles');
echo $date->format('I');
# 1

$date = new DateTime('09-Mar-2013 America/Los_Angeles');
echo $date->format('I');
# 0

$date = new DateTime('10-Mar-2013 America/Los_Angeles');
echo $date->format('I');
# 0

请注意,最后一个DST仍然为0?这是因为转换发生在凌晨2:00:

Notice that the DST is still 0 on that last one? That's because the transition happens at 2:00 AM:

$date = new DateTime('10-Mar-2013 01:00 America/Los_Angeles');
echo $date->format('I');
# 0

$date = new DateTime('10-Mar-2013 02:00 America/Los_Angeles');
echo $date->format('I');
# 1

这是一个弹跳式转换,时钟从2:00至3:00。

This is a "spring-forward" transition, where the clock jumps from 2:00 to 3:00.

在回退过渡期间小心模糊,时钟从2:00返回到1:00

Beware of ambiguity during fall-back transitions, where the clock jumps from 2:00 back to 1:00

$date = new DateTime('3-Nov-2013 01:00 America/Los_Angeles');
echo $date->format('I');
# 1

有两个1:00 AM,这是第一个。这是不明确的,因为我们可能意味着第二个,而不是代表。

There are two 1:00 AMs, and this is taking the first one. It is ambiguous, because we might have meant the second one, and that is not represented.

人们会认为PHP将允许以下任一项:

One would think that PHP would allow either of the following:

new DateTime('3-Nov-2013 01:00 -0700 America/Los_Angeles')  #PDT
new DateTime('3-Nov-2013 01:00 -0800 America/Los_Angeles')  #PST

但这些不我测试时似乎工作。我也尝试过ISO格式的日期。任何人都知道如何正确区分PHP中的模糊值?如果是,请在评论中编辑或更新。谢谢。

But these don't seem to work when I tested. I also tried ISO format dates. Anyone know how to properly distinguish ambiguous values in PHP? If so, please edit or update in comments. Thanks.