php-如何检查DST夏时制中的时间戳?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php-如何检查DST夏时制中的时间戳?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

问题是,例如,在某些时区,秋季2:00 am或2:59 am在秋天翻了一番,而在夏令时节省时间的更改中春天则不存在.

如果我在全年的每一分钟都运行一个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
    ...

解决方法:

如果您使用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)

脚本宝典总结

以上是脚本宝典为你收集整理的php-如何检查DST夏时制中的时间戳?全部内容,希望文章能够帮你解决php-如何检查DST夏时制中的时间戳?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: