在 php 中显示多个时区或时区时钟
我在现场有一位演示者和观众在全球范围内进行协作 他们都是注册用户,其位置(城市/州/国家)存储在 mysql 中,
会议时间根据其中一个用户的当地时间在 php 上向两个用户显示,我希望其他用户的时间根据第一个用户的位置进行相应调整。 (请记住美国的夏令时)
或者
我希望所有美国时区自动更新时钟显示在网页上,
如何做到这一点?
I have a presenter and viewer on site collaborating globally
They are both registered users with their location (city/state/country) stored in mysql
meeting times are displayed to both users on php based on one of the users local time, i would like other user's time adjusted accordingly based on first user's location. (keeping in mind daylight saving in USA)
or alternatively
i would like all USA timezone self updating clocks displayed on the webpage
how can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以 UTC 格式存储全球所有时间。显示时间时,首先找出用户所在的时区(客户端或服务器端),然后通过 php 或 javascript 相应地更改时间。设置时间时,请确保将用户时区排除在外,并将其再次存储为 UTC。
Store all times globally in UTC. When displaying time, first find out what timezone the user is (client side or server side) and change the time accordingly through php or javascript. When setting time, ensure that you also take the users timezone out of the equation and store it as UTC again.
这可以通过 javascript 和(cookie 女巫可以保存客户端时区,或 ajax 将其发送回服务器)来完成。然后您可以在 PHP 脚本上使用它来进行调整。在 JavaScript 中使用 getTimezoneOffset() 方法。这是一个使用它的简单脚本:
This could be done via javascript and (cookie witch could save the client timezone, or ajax to send it back to the server). Then you could use it on PHP script to make the adjustments. In javascript use getTimezoneOffset() method. Here is a simple script for using it:
在数据库中,我将以 UTC 格式存储原始会议时间。我还会在用户表上有一个字段来分配 TZ 类别。
夏令时的简单 if/then 在该日期处于活动状态,然后转换时间。
我会使用 php 来完成这一切。因为您可以组合 db pull 以在同一查询中包含 UTC + TZ 更改。然后,如有必要,请调整夏令时。
In the database, I would store the original meeting time in UTC. I would also have a field on the user table to assign a TZ category.
A simple if/then for daylight savings is active on that date, and then convert the time.
I'd use php for all of it. Since you can combine the db pull to include UTC + TZ alteration in the same query. Then, adjust for daylight savings if necessary.