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


基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01