如何查找给定时区的 DST 开始和结束日期


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

"美国/纽约"的时钟变化:
当地夏令时即将到
来时 星期日, 3 十一月 2013, 02:00:00 时钟倒退 1 小时至
2013 年 11 月 3 日星期日,当地标准时间 01:00:00

"欧洲/柏林"的时钟变化:
当地夏令时即将到
来时 星期日, 27 十月 2013, 03:00:00 时钟倒退 1 小时至
2013 年 10 月 27 日星期日,当地标准时间 02:00:00

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

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

我认为getTransitions是你所追求的:

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

我承认,这有点碍眼,如果你对为什么数组中返回多个条目感到困惑,那是因为确切的日期不同,因为在大多数地区,它基于一个月中的星期几(例如"十月的最后一个星期日")而不是特定的日期。对于上述内容,如果您只想要即将到来的过渡,您将添加 timestamp_being 参数:

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

有了getTransitions,你就可以得到所有的转换(从 php 5.3 开始和结束)

这将在 PHP <5.3 中工作

<?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);
}