문제

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

How do I get these dates with PHP?
For example: how do I get the date "Sunday, 27 October 2013, 02:00:00" for Berlin 2014 without google ;)

And if I have a unixtimestamp that lies inside that hour, will it point to the first or the last hour?

도움이 되었습니까?

해결책

I think getTransitions is what you're after:

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

It's a bit of an eyesore to look at, I admit, and if you're confused as to why there are multiple entries returned in the array, it's because the exact date is different because in most regions it's based on the day of the week of a month (such as "last Sunday in October") not a specific date. For the above if you only wanted the upcoming transitions, you would add the timestamp_being argument:

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

다른 팁

With getTransitions you get all transitions (since php 5.3 with begin and end)

This will work in 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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top