在 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?
编辑澄清:
我想知道是否会有一些现有的方法,或者实现这样的方法的方法:
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.
这篇关于如何判断时区是否在一年中的任何时候都遵守夏令时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!