如何判断一个时区是否在一年的任何时间都能观看夏令时? [英] How to tell if a timezone observes daylight saving at any time of the year?
问题描述
在PHP中,您可以通过使用以下方式来确定给定日期是否在夏令时期间:
In PHP, you can tell if a given date is during the Daylight Savings Time period by using something like this:
$isDST = date("I", $myDate); // 1 or 0
问题是这只会告诉你这一个时间点是否在DST。有没有可靠的方法检查DST是否在该时区的任何时间生效?
The problem is that this only tells you whether that one point in time is in DST. Is there a reliable way to check whether DST is in effect at any time in that timezone?
编辑为澄清:
- 澳大利亚布里斯班在一年的任何时候都没有观察到夏令时。全年都是GMT + 10。
- 澳大利亚悉尼从10月到3月,从GMT + 10变为GMT + 11。
我想知道是否会有一些现有的方法,或者一种实现一种如此工作的方法:
I'm wondering if there would be some existing method, or a way to implement a method which works as such:
timezoneDoesDST('Australia/Brisbane'); // false
timezoneDoesDST('Australia/Sydney'); // true
推荐答案
我找到了一种方法使用PHP的DateTimezone类(PHP 5.2 +)
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()));
}
或者,如果您正在运行PHP 5.3 +
or, if you're running PHP 5.3+
function timezoneDoesDST($tzId) {
$tz = new DateTimeZone($tzId);
return count($tz->getTransitions(time())) > 0;
}
getTransitions()
功能为您提供有关每个时区的偏移量更改的信息。这包括历史数据(布里斯本在1916年有夏令时..谁知道?),所以这个功能检查未来是否有偏移变化。
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.
这篇关于如何判断一个时区是否在一年的任何时间都能观看夏令时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!