如何判断时区是否在一年中的任何时候都遵守夏令时?
在 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 在该时区的任何时间是否有效?
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.
相关文章