简体   繁体   English

服务器和浏览器之间的时间同步

[英]Time sync between server & browser

I am working on a live feed for a company which displays the status of their production schedule over the current 24 hour period. 我正在为一家公司提供实时供稿,该公司可以显示其在当前24小时内的生产进度状态。 The issue is that some of the employees need to be able to view this live feed from abroad and in different time zones. 问题在于,一些员工需要能够从国外和不同时区查看此实时提要。

There are 2 elements which need to display the current time "on-site" (server): 有两个元素需要在“现场”(服务器)上显示当前时间:

  • A clock which must display the current time on-site. 必须在现场显示当前时间的时钟。

    时钟

  • A progress bar which shows the progress through the current 24 hour period on-site. 进度条显示现场当前24小时内的进度。

    进度条

I know that synchronizing the time live is inefficient. 我知道同步直播时间效率很低。 I'm not sure about how to go about offsetting the time on the client to match the server because I could encounter a number of issues: 我不确定如何偏移客户机上的时间来匹配服务器,因为我可能会遇到很多问题:

  • Daylight savings time in either location 在任何一个位置的夏令时
  • Timezone ahead or behind the server 服务器之前或之后的时区

If anyone has any experience/ideas to help it would be much appreciated. 如果有人有任何经验/想法可以帮助您,将不胜感激。

Thanks 谢谢

Server time should be in UTC. 服务器时间应为UTC。 That way, it's easier to move timezones. 这样,移动时区就更容易了。

Don't rely on the client for timezone since it depends on the client's machine, which could be set to any time and timezone. 不要依赖客户端的时区,因为它取决于客户端的计算机,可以将其设置为任何时间和时区。 Have an account that requires a timezone. 有一个需要时区的帐户。 Use that for timezone offsets. 将其用于时区偏移量。 Same reason why online services ask you your timezone instead of detecting it. 联机服务询问您的时区而不是检测时区的原因相同。

Then you can either go for interval sync, or sockets. 然后,您可以进行间隔同步或套接字。 Do note that latency could delay it by a few seconds. 请注意,延迟可能会将其延迟几秒钟。

Interval sync 间隔同步

You can have the server provide the initial time and timezone offset. 您可以让服务器提供初始时间和时区偏移量。 Calculate the client's initial time based on those initial data, and have the client-side do the ticking. 根据这些初始数据计算客户端的初始时间,并让客户端进行报价。 Since the script can go out of sync, you sync to the server periodically, like every 5 mins or so. 由于脚本可能不同步,因此您可以定期同步到服务器,例如每5分钟左右一次。

Sockets 插座

Have the server calculate the user's time at his timezone, and send it down the line via sockets every second. 让服务器计算用户所在时区的时间,然后每秒通过套接字将其发送到线路上。 All you need to do in the client side is display it. 在客户端需要做的就是显示它。

Directly from the MDN website : 直接从MDN网站

var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;

This will get you the timezone offset in minutes between UTC and local time (the above example divides by 60 to convert to hours). 这将使您获得UTC与本地时间之间的时区偏移量(以分钟为单位)(上面的示例除以60转换为小时)。 This will already account for daylight savings time. 这已经可以解决夏令时了。

If your server puts the server timezone offset in a variable in the page or sends it with the live data or if the live data is in UTC time, then you can calculate the difference between the client and the data. 如果服务器将服务器时区偏移量放置在页面中的变量中,或者将其与实时数据一起发送,或者实时数据以UTC时间为单位,则可以计算客户端与数据之间的差。

Give your users the option to select the timezone they are in (via a dropdown or options page) and set it with php's native function date_default_timezone_set. 给您的用户一个选项,以选择他们所在的时区(通过下拉菜单或选项页),并使用php的本机函数date_default_timezone_set进行设置。

date_default_timezone_set("Europe/Brussels"); date_default_timezone_set(“欧洲/布鲁塞尔”);

Then the time difference between the host server and the user is calculated automatically and daylight savings is also taken care of. 然后,将自动计算主机服务器与用户之间的时间差,并同时考虑夏令时。

The documentation is here: https://php.net/manual/en/timezones.php 该文档在这里: https : //php.net/manual/en/timezones.php

Then display the times using php's date function: 然后使用php的date函数显示时间:

date("c"); date(“ c”); // ISO 8601 date // ISO 8601日期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM