Javascript / PHP和时区 [英] Javascript/PHP and timezones

查看:98
本文介绍了Javascript / PHP和时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够猜测用户的时区偏移量以及是否应用了夏令时。目前,我发现的最明确的代码是:

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脚本中使用这些以便输出本地用户的日期/时间....但最好的是什么?我想我有2个选项:

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重新检查时区,以便捕获时间用户的时区变化(非常不可能)或者他们传递到夏令时期间。

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.

它将为您提供Olsen时区数据库密钥您可以用于服务器端日期时间计算。

It will give you an Olsen timezone database key that you can use for server side datetime calculations.

这篇关于Javascript / PHP和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆