问题描述
我希望能够猜测用户的时区偏移量以及是否应用了夏令时.目前,我为此找到的最权威的代码在这里:
I'd like to be able to guess the user's timezone offset and whether or not daylight savings is being applied. Currently, the most definitive code that I've found for this is here:
http://www.michaelapproved.com/articles/daylight-saving-time-dst-detect/
所以这给了我与 DST 指示器一起的偏移量.
So this gives me the offset along with the DST indicator.
现在,我想在我的 PHP 脚本中使用这些,以便为用户输出本地日期/时间....但是什么是最好的呢?我想我有两个选择:
Now, I want to use these in my PHP scripts in order to ouput the local date/time for the user....but what's best for this? I figure I have 2 options:
a) 从 timezone_abbreviations_list() 的输出中选择一个具有相同偏移量和 DST 设置的随机时区.然后调用 date_timezone_set() 以便对时间应用正确的处理.
a) Pick a random timezone which has the same offset and DST setting from the output of timezone_abbreviations_list(). Then call date_timezone_set() with this in order to apply the correct treatment to the time.
b) 继续将日期视为 UTC,但只需添加一些时间戳以添加适当的小时数.
b) Continue treating the date as UTC but just do some timestamp addition to add the appropriate number of hours on.
我的感觉是选项 B 是最好的方法.这样做的原因是,对于 A,我可以使用一个时区,虽然它在偏移量/dst 方面是正确的,但可能在幕后有一些模糊的规则,可能会产生令人惊讶的结果(我不知道任何但尽管如此我不认为我可以排除它).
My feeling is that option B is the best way. The reason for this is that with A, I could be using a timezone which although correct in terms of offset/dst, may have some obscur rules in place behind the scene that could give surprising results (I don't know of any but nonetheless I don't think I can rule it out).
然后,我会在每个会话开始时使用 Javascript 重新检查时区,以便捕捉用户的时区何时更改(非常不可能)或他们进入 DST 期间.
I'd then re-check the timezone using Javascript at the start of each session in order to capture when either the user's timezone changes (very unlikely) or they pass in to the DST period.
很抱歉,我的大脑转储 - 我真的只是在某种保证上述方法有效之后.
Sorry for the brain dump - I'm really just after some sort of reassurance that the approaches above are valid.
谢谢,
詹姆斯.
推荐答案
使用 javascript 稳健地确定用户的时区.查看 jsTimezoneDetect.
To robustly determine a user's timezone using javascript. Check out jsTimezoneDetect.
它将为您提供一个奥尔森时区数据库密钥,您可以将其用于服务器端日期时间计算.
It will give you an Olsen timezone database key that you can use for server side datetime calculations.
这篇关于Javascript/PHP 和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!