1

I was wondering if there is any API/way/site/link that provides the correct time?

Not the servers datetime or the clients datetime. I'm using datetime to count down on my gaming site for when it is the user's time to make a play. Users come from all over the world, so using their client time would not match if it's from the US to Europe.

Then normally I would use the servers time, but somehow it skips 1.2 hours sometimes? I would like to make sure that everbody makes a timestamp from the same source and that source is always correct!

Any ideas?

4

2 回答 2

0

首先,您应该在客户端和服务器代码中使用UTC以避免时区问题。

此外,我以某种方式理解您的问题,即时间准确(纳秒)正确并不重要,更重要的是,服务器和客户端上的时间相同。

因此,客户端和服务器都需要该 UTC 时间的唯一来源。这绝对应该是您的服务器。我可以建议将您的服务器用作客户端的 NTP 服务器。但是假设您在网络环境中,这听起来不切实际。

所以我建议为来自服务器的每条消息添加一个时间戳 - UTC 时间。客户必须使用此时间戳作为参考。Additonaly 可以让客户端通过调用你的服务器来定期更新他们的时间。

于 2012-12-19T17:10:58.560 回答
0

使用服务器的时间很好,只需确保正确获取时区:

$now = new DateTime;
$now->setTimeZone(new DateTimeZone("Europe/London"));

现在,$now将包含一个 DateTime 对象,以及伦敦的正确时间。

PHP 在夏令时和奇怪的自定义时间事件方面做得很好。

于 2012-12-19T17:13:38.590 回答