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 在该时区的任何时间是否有效?
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.
这篇关于如何判断时区是否在一年中的任何时候都遵守夏令时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何判断时区是否在一年中的任何时候都遵守夏令时?


- 没有作曲家的 PSR4 自动加载 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 仓库 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01