PHP 函数 time() 的时间跳跃 [英] Leap in time in PHP's function time()

查看:37
本文介绍了PHP 函数 time() 的时间跳跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PHP 的 time() 函数的时间戳,我使用 date() 向用户显示当前时间.

Using the timestamp of PHP's time() function, I show the current time to the user with date().

格式:H:i => 13:57

Format: H:i => 13:57

但有时似乎时间有飞跃.当我访问该页面时,我的 Windows 时钟显示 13:40.但我的网站说 14:XX.我已经不知道确切时间了.

But sometimes there seem to be leaps in time. My windows clock was showing 13:40 when I visited the page. But my website said 14:XX. I don't know the exact time any more.

另一个例子:用户访问了谁在线?"页面.在 00:16.列出了一个用户,他最后一次操作的时间是 00:39.每次加载页面时,都会将上次操作时间写入数据库.所以 time() 函数肯定在 00:16 返回了 00:39 并且错误的值肯定已经写入数据库.

Another example: A user accessed the page "Who is online?" at 00:16. A user was listed with the time for his last action being 00:39. The last action time is written to the database on every page load. So the time() function must have given back 00:39 at 00:16 and the wrong value must have been written to the database.

怎么会这样?

非常感谢您的帮助:)

编辑 #1 我已将代码缩减为对这个问题很重要的部分:

Edit #1 I've reduced my code to the parts which are important for this problem:

我在 serverData.php 中使用 $tStamp = time() 将时间戳保存到一个新变量中,该文件包含在 index.php 中,我在其中使用 date('dmY, H:i',$tStamp).变量 $tStamp 在任何其他行中都没有改变.

I save the timestamp to a new variable with $tStamp = time() in serverData.php and this file is included in index.php where I show the time to the user with date('d.m.Y, H:i', $tStamp). The variable $tStamp isn't changed in any other line.

编辑 #2 我网站上的用户注意到 2010-01-21 00:16 时间错误,其中页面在很短的时间内显示 00:39.我查看了日志文件,大约在这个时候,日志文件中缺少一个完整的小时:

Edit #2 Users on my website noticed a wrong time on 2010-01-21 00:16 where the page showed 00:39 for a very short moment. I've had a look into the log files and around this time there's one complete hour missing in the logfiles:

127.0.0.1 - - [20/Jan/2010:23:34:53 +0100] ...

127.0.0.1 - - [20/Jan/2010:23:34:53 +0100] ...

127.0.0.1 - - [20/Jan/2010:23:34:55 +0100] ...

127.0.0.1 - - [20/Jan/2010:23:34:55 +0100] ...

127.0.0.1 - - [21/Jan/2010:00:38:41 +0100] ...

127.0.0.1 - - [21/Jan/2010:00:38:41 +0100] ...

127.0.0.1 - - [21/Jan/2010:00:38:41 +0100] ...

127.0.0.1 - - [21/Jan/2010:00:38:41 +0100] ...

我的主机发布了 2010-01-20 00:00-06:00 的维护工作.你认为维护被推迟了吗?难道这个时间段的维护工作是在第二天完成的吗?这样的工作会不会弄错时间?

My hoster announced maintenance work for 2010-01-20 00:00-06:00. Do you think that the maintenance was set back? Could it be the case that the maintenance work was done on the next day in this time period? Could such a work make the time wrong?

编辑 #3 最后,我从我的托管人那里得到了答复 :) 在给定的时间内,服务器崩溃了.正因为如此,手表/时钟停止了.就是这么简单,但我没想到服务器崩溃.非常感谢大家!

Edit #3 Finally, I have an answer from my hoster :) In the given period of time, the server crashed. And because of that the watch/clock stopped. It was that simple but I didn't think of a server crash. Thank all of you very much!

推荐答案

在阅读了所有这个问题以及您在评论中的回答后,我可以说这是 PHP 中的错误的可能性非常小.PHP 的 time() 直接从 OS 拉取,而 OS 直接从主板上的硬件时钟拉取.由于您使用的是共享主机,如果这种情况经常发生足以成为问题,您将不得不要求您的主机进行调查.它可能是一个流氓 NTP 服务器或进程,也可能是硬件时钟的某种短路——无法访问服务器日志或物理访问计算机,据我所知,您个人可以做的事情并不多.

Having read all of this question and your responses in comments, I can say that the chances of this being a bug in PHP are pretty infinitesimal. PHP's time() pulls directly from the OS, which pulls directly from the hardware clock on the mainboard. Since you're on shared hosting, you're going to have to ask your host to look into it if this is happening often enough to be a problem. It could be a rogue NTP server or process, it could be some sort of short in the hardware clock — without access to the server logs or physical access to the computer, there's not a whole lot that you can do personally that I know of.

这篇关于PHP 函数 time() 的时间跳跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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