如何判断时区是否在一年中的任何时候都能观察到夏令时?

nic*_*ckf 12 php timezone datetime dst

在PHP中,您可以通过使用以下内容来判断给定日期是否在夏令时期间:

$isDST = date("I", $myDate); // 1 or 0
Run Code Online (Sandbox Code Playgroud)

问题是,这只会告诉您一个时间点是否在夏令时.是否有可靠的方法来检查DST是否在该时区的任何时间生效?


编辑澄清:

  • 澳大利亚布里斯班在一年中的任何时候都没有观察到夏令时.全年都是GMT + 10.
  • 澳大利亚悉尼从10月到3月,从GMT + 10变为GMT + 11.

我想知道是否会有一些现有的方法,或者一种方法来实现一个如此工作的方法:

timezoneDoesDST('Australia/Brisbane');  // false
timezoneDoesDST('Australia/Sydney');  // true
Run Code Online (Sandbox Code Playgroud)

nic*_*ckf 14

我找到了一个使用PHP的DateTimezone类(PHP 5.2+)工作的方法

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

或者,如果您运行PHP 5.3+

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

getTransitions()功能为您提供每次偏移时区变化的信息.这包括历史数据(布里斯班在1916年有夏令时......谁知道?),因此该功能检查未来是否存在偏移变化.


小智 10

实际上,nickf方法对我没有用,所以我稍微改了一下......

/**
* 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
Run Code Online (Sandbox Code Playgroud)

相同的函数调用将在1个月(3月)返回true,希望它有所帮助