php PHP夏令时检测
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6155224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP daylight saving time detection
提问by Nicolas
I need to send an email to users based wherever in the world at 9:00 am local time. The server is in the UK. What I can do is set up a time difference between each user and the server's time, which would then perfectly work if DST didn't exist.
我需要在当地时间上午 9:00 向世界各地的用户发送电子邮件。服务器在英国。我能做的是设置每个用户和服务器时间之间的时差,如果 DST 不存在,这将完美地工作。
Here's an example to illustrate it:
下面是一个例子来说明它:
- John works in New York, -5 hours from the server (UK) time
- Richard works in London, UK, so 0 hour difference with the server.
- John 在纽约工作,距离服务器(英国)时间 -5 小时
- 理查德在英国伦敦工作,因此与服务器的时差为 0。
When the server goes from GMT to GMT +1 (BST) at 2:00am on a certain Sunday, this means that John now has a -6H time difference now.
当服务器在某个星期日凌晨 2:00 从 GMT 转到 GMT +1 (BST) 时,这意味着 John 现在有 -6H 的时差。
This scenario I can still handle by updating all the users outside the server's local time, but once I've moved forward/backward the time of all the other users, I still need a way to detect when(time and date) the users living outside the UK will (or will not) change their local time to a probable DST one.
这种情况下我仍然可以通过更新所有用户的服务器的本地时间之外处理,但一旦我前进/后退的所有其他用户的时候,我还需要一种方法来检测时(时间和日期)的用户生活英国以外的人将(或不会)将其当地时间更改为可能的夏令时时间。
I need a PHP method to know/detect when other parts of the world will enter/exit DST.
我需要一个 PHP 方法来知道/检测世界其他地方何时进入/退出 DST。
采纳答案by squirrel
Do you need to know all the details of DST transition yourself? or do you just need to know when is 9:00 am in a given timezone?
您是否需要自己了解 DST 过渡的所有细节?或者您只需要知道给定时区的上午 9:00 是什么时候?
If it's the latter, PHP can use your operating system's timezone database to do that for you. The strtotime()function is remarkably good at "figuring out" what you mean:
如果是后者,PHP 可以使用您操作系统的时区数据库为您完成这项工作。该的strtotime()函数在“搞清楚”你的意思是非常好:
echo strtotime("today 9:00 am America/New_York"); // prints "1306501200"
echo strtotime("today 9:00 am Europe/London"); // prints "1306483200"
Just make sure you're using one of the PHP supported timezones.
只要确保您使用的是PHP 支持的时区之一。
回答by Owen
As Jimmy points out you can use timezone transitions, but this is not available on PHP <5.3. as dateTimeZone() is PHP>=5.2.2 but getTransitions() with arguments is not! In that case here is a function that can give you timezone data, including whether in DST or not.
正如吉米指出的那样,您可以使用时区转换,但这在 PHP <5.3 上不可用。因为 dateTimeZone() 是 PHP>=5.2.2 但带参数的 getTransitions() 不是!在这种情况下,这里有一个函数可以为您提供时区数据,包括是否在 DST 中。
function timezonez($timezone = 'Europe/London'){
$tz = new DateTimeZone($timezone);
$transitions = $tz->getTransitions();
if (is_array($transitions)){
foreach ($transitions as $k => $t){
// look for current year
if (substr($t['time'],0,4) == date('Y')){
$trans = $t;
break;
}
}
}
return (isset($trans)) ? $trans : false;
}
Having said that, there is a simpler method using date() if you just need to know whether a timezone is in DST. For example if you want to know if UK is in DST you can do this:
话虽如此,如果您只需要知道时区是否在 DST 中,还有一种使用 date() 的更简单方法。例如,如果您想知道英国是否在夏令时,您可以这样做:
date_default_timezone_set('Europe/London');
$bool = date('I'); // this will be 1 in DST or else 0
... or supply a timestamp as a second arg to date() if you want to specify a datetime other than your current server time.
...或者如果要指定当前服务器时间以外的日期时间,则为 date() 提供时间戳作为第二个参数。
回答by Jimmy Sawczuk
Changing my answer a bit: DateTimeZone::getTransitionslooks like it will do what you need, provided you have PHP >= 5.2.
稍微改变我的答案:DateTimeZone::getTransitions看起来可以满足您的需求,前提是您的 PHP >= 5.2。
From a comment in the documentation:
从文档中的评论:
<?php
$theTime = time(); // specific date/time we're checking, in epoch seconds.
$tz = new DateTimeZone('America/Los_Angeles');
$transition = $tz->getTransitions($theTime, $theTime);
// only one array should be returned into $transition. Now get the data:
$offset = $transition[0]['offset'];
$abbr = $transition[0]['abbr'];
?>
So here, all we need to do is pass in the timezone we want to check and we can know if that timezone is in DST/what the offset is. You'll then need to check the offset against GMT to see if you want to send your e-mail now, or not now.
所以在这里,我们需要做的就是传入我们想要检查的时区,我们可以知道该时区是否在夏令时/偏移量是多少。然后,您需要根据 GMT 来检查偏移量,以查看是否要立即发送电子邮件,或者不立即发送。