How to tell if a timezone observes daylight saving at any time of the year?

ぃ、小莉子 提交于 2019-11-27 07:53:56

I've found a method which works using PHP's DateTimezone class (PHP 5.2+)

function timezoneDoesDST($tzId) {
    $tz = new DateTimeZone($tzId);
    $trans = $tz->getTransitions();
    return ((count($trans) && $trans[count($trans) - 1]['ts'] > time()));
}

or, if you're running PHP 5.3+

function timezoneDoesDST($tzId) {
    $tz = new DateTimeZone($tzId);
    return count($tz->getTransitions(time())) > 0;
}

The getTransitions() function gives you information about each time the offset changes for a timezone. This includes historical data (Brisbane had daylight savings in 1916.. who knew?), so this function checks if there's an offset change in the future or not.

hertzel

Actually nickf method didn't works for me so I reworked it a little ...

/**
* Finds wherever a TZ is experimenting dst or not
* @author hertzel Armengol <emudojo @ gmail.com>
* @params string TimeZone -> US/Pacific for example
*
*/
function timezoneExhibitsDST($tzId) {
    $tz = new DateTimeZone($tzId);
    $date = new DateTime("now",$tz);  
    $trans = $tz->getTransitions();
    foreach ($trans as $k => $t) 
      if ($t["ts"] > $date->format('U')) {
          return $trans[$k-1]['isdst'];    
    }
}

// Usage  

var_dump(timezoneExhibitsDST("US/Pacific")); --> prints false
var_dump(timezoneExhibitsDST("Europe/London")); --> prints false
var_dump(timezoneExhibitsDST("America/Chicago")); --> prints false

same function call will return true in 1 month (March) hope it helps

DateTimeZone::getTransitions might help.

You could probably wing it:

$hasDst = date("I", strtotime('June 1')) !== date("I", strtotime('Jan 1'));

Otherwise you'd need to parse the text-based zoneinfo data files.

I don't think so, but since almost every country that observes DST changes its time for an entire season or two, you could try to test 4 points during any given year.

For example, test date("I", $date) for 2009/01/01, 2009/04/01, 2009/07/01 and 2009/10/01. If that timezone falls into DST, then at least one of those dates will return 1.

date has to be on the user/server timezone for it to work, and you can't use a range with date as you do with getTransitions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!