关于php:如何查找DST在给定时区开始和结束的日期?

How to lookup the dates that DST starts and ends for a given timezone?

Clock changes in"America/New York":
When local daylight time was about to reach
Sunday, 3 November 2013, 02:00:00 clocks were turned backward 1 hour to
Sunday, 3 November 2013, 01:00:00 local standard time instead

Clock changes in"Europe/Berlin":
When local daylight time was about to reach
Sunday, 27 October 2013, 03:00:00 clocks were turned backward 1 hour to
Sunday, 27 October 2013, 02:00:00 local standard time instead

如何使用PHP获取这些日期?
例如:如何在没有谷歌的情况下获取2014年柏林2014年10月27日星期日02:00:00的日期;)

如果我有一个位于该小时内的unixtimestamp,它会指向第一个还是最后一个小时?


我认为getTransitions就是你所追求的:

1
2
$timezone = new DateTimeZone("Europe/London");
$transitions = $timezone->getTransitions();

我承认,看起来有点眼花缭乱,如果你对为什么在数组中返回多个条目感到困惑,那是因为确切的日期不同,因为在大多数地区它都是基于星期几 一个月(例如"十月的最后一个星期天")不是特定的日期。 对于上面的内容,如果您只想要即将到来的转换,则可以添加timestamp_being参数:

1
2
$timezone = new DateTimeZone("Europe/London");
$transitions = $timezone->getTransitions(time());

使用getTransitions,您将获得所有转换(从php 5.3开始和结束)

这将在PHP <5.3中工作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
/** returns an array with two elements for spring and fall DST in a given year
 *  works in PHP_VERSION < 5.3
 *
 * @param integer $year
 * @param string $tz timezone
 * @return array
 **/

function getTransitionsForYear($year=null, $tz = null){
    if(!$year) $year=date("Y");

    if (!$tz) $tz = date_default_timezone_get();
    $timeZone = new DateTimeZone($tz);

    if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
        $transitions = $timeZone->getTransitions(mktime(0, 0, 0, 2, 1, $year),mktime(0, 0, 0, 11, 31, $year));
        $index=1;
    } else {
        // since 1980 it is regular, the 29th element is 1980-04-06
            // change this in your timezone
            $first_regular_index=29;
            $first_regular_year=1980;
        $transitions = $timeZone->getTransitions();
        $index=($year-$first_regular_year)*2+$first_regular_index;
    }
    return array_slice($transitions, $index, 2);
}