如何检查时间戳是否在夏令时夏令时?

时间:2013-11-13 05:19:05

标签: php dst

问题是,例如凌晨2点或凌晨2点59分,在某些时区秋季加倍,而在夏令时省时间变化期间春季则不存在。

如果我在整整一年中每分钟运行一次PHP循环,我怎样才能在循环中捕获DST节省时间? (在当前时区中,由date_default_timezone_set设置)

我如何完成PHP 5.2兼容功能,如:

<?php
/** returns true if a time is skipped or doubled 
 * (for example "2013-03-10 02:30" doesen't exist in USA)
 * 
 * @param string $season
 * @param string $datestring in the form of "2013-03-10 02:30"
 * @return boolean
 **/
function checkDST($datestring,$season="any"){
    $tz=date_default_timezone_get();
    $season=strtolower(trim($season));
    if($season=="spring"){
        if(/* an hour skipped */) return true;  
    }else if($season=="autumn"){
        if(/* double hour */) return true;  
    } else if($season=="any") {
        if(/* any of both */) return true;
    }
    return false
}

所以我可以使用

date_default_timezone_set("America/New_York");
$is_skipped=checkDST("2013-03-10 02:30","spring");  

$exists_two_times=checkDST(2003-11-03, 02:00,"autumn"); // or 2:59 for example

(时区见:http://www.timeanddate.com/worldclock/clockchange.html?n=224&year=2013


编辑:
我发现了如何检测弹簧DST:

function checkDST($datestring,$season="any"){
    var_dump('checking '.$datestring);
    $season=strtolower(trim($season));
    $datestring=substr($datestring,0,16);
    if($season!="autumn" and date("Y-m-d H:i",strtotime($datestring))!=$datestring) {
        return true;
    }
    // check for double hours in autumn
    ...

2 个答案:

答案 0 :(得分:3)

如果您使用PHP的date_default_timezone_set来解析我认为是Unix时间戳的内容,那么时间戳将相对于该时区进行解析。然后确定夏令时间偏移(如果相关)use DateTimeZone::getTransitions

与您示例中的函数类似的函数可能如下所示:

<?php
function checkDST($datestring, $season, $tz = "America/New_York", $minutes_from_now_to_check = 1){
    $seconds_to_check = ($minutes_from_now_to_check * 60) + 30;
    if (!$tz) $tz = date_default_timezone_get();
    $timestamp = strtotime($datestring);
    $timestamp_start = new DateTime();
    $timestamp_start->setTimestamp($timestamp);
    $timestamp_end = new DateTime();
    $timestamp_end->setTimestamp($timestamp)->add(new DateInterval('PT'.$seconds_to_check.'S'));

    $timezone = new DateTimeZone($tz);
    $transitions = $timezone->getTransitions($timestamp_start->getTimestamp(), $timestamp_end->getTimestamp());
    if (count($transitions) > 1) { // there's an imminent DST transition, spring or fall
        if (strtolower($season) == "spring" && $transitions[1]["isdst"] === true){
            return true;    
        } 
        if (strtolower($season)=="autumn" && $transitions[1]["isdst"] === false){
            return true;
        }
        if (strtolower($season)=="any"){
            return true;
        }
    }
    return false;
}

$is_skipped = checkDST("2013-03-10 01:59","spring");
var_dump($is_skipped); // will display bool(true)

$is_skipped = checkDST("2013-03-10 12:30","spring");
var_dump($is_skipped); // will display bool(false)

$exists_two_times=checkDST("2013-11-03 01:59","autumn");
var_dump($exists_two_times); // bool(true)

$exists_two_times=checkDST("2013-11-03 03:00","autumn");
var_dump($exists_two_times); // bool(false)

答案 1 :(得分:0)

这一直适用于

date_default_timezone_set("America/New_York");

只有一个错误,如果我把它称为欧洲,那么它的时间是提前一小时:

date_default_timezone_set("Europe/Berlin");
var_dump(checkDST("2013-10-27 01:30","any"));
var_dump(checkDST("2013-10-27 02:30","any"));

(欧洲的储蓄时间是从凌晨2点到凌晨3点)

这是迄今为止的结论:

<?php
/** returns true if a time is skipped or doubled 
 * (for example "2013-03-10 02:30" doesen't exist in USA)
 * 
 * @param string $season "spring", "autumn", "fall" or any other string for any of the time changes
 * @param string $datestring in the form of "2013-03-10 02:30"
 * @return boolean
 **/
function checkDST($datestring, $season, $tz = null){
    $debug=true;
    if($debug) echo ('checking '.$season.' '.$datestring);
    $season=strtolower(trim($season));
    if($season=="fall") $season="autumn";
    $datestring=substr($datestring,0,16);
    $monthdaystring=substr($datestring,0,10);
    if(!strtotime($datestring) or date("Y-m-d",strtotime($monthdaystring))!=$monthdaystring) {
        //Error
        if($debug) echo "<br>Error: not a correct datestring: $datestring";
        return false;
    }
    if($season!="autumn" and date("Y-m-d H:i",strtotime($datestring))!=$datestring) {
        // spring or any
        if($debug) echo "<br>".(date("Y-m-d H:i",strtotime($datestring)).'!='.$datestring);
        return true;
    } else if($season=="spring") {
        // spring ends here
        return false;
    }
    // "autumn" or "any" 
    $timestamp = strtotime($datestring);

    $timestamp_start = new DateTime();
    $timestamp_start->setTimestamp($timestamp);
    $timestamp_end = new DateTime();
    $timestamp_end->setTimestamp($timestamp)->add(new DateInterval('PT3600S'));

    if (!$tz) $tz = date_default_timezone_get();
    $timezone = new DateTimeZone($tz);
    $transitions = $timezone->getTransitions($timestamp_start->getTimestamp(), $timestamp_end->getTimestamp());
    if (count($transitions) > 1) { // there's an imminent DST transition, spring or fall
        // autumn
        if($debug) echo "<br>NumTransitions: ".(count($transitions));
        if($debug) var_dump($transitions);
        return true;
    }
    return false;
}