Leap in time in PHP's function time()
Using the timestamp of PHP's time() function, I show the current time to the user with date().
Format: H:i => 13:57
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 开发者_如何学JAVAknow the exact time any more.
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.
How could this happen?
Thank you very much for your help :)
Edit #1 I've reduced my code to the parts which are important for this problem:
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.
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:55 +0100] ...
127.0.0.1 - - [21/Jan/2010:00:38:41 +0100] ...
127.0.0.1 - - [21/Jan/2010:00:38:41 +0100] ...
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?
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!
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.
As a really far out option, there could be a bad ntp location that is temporarily changing the system time when it is checked against. It would be set back to the correct time on the next update to a correct server. I don't think a system time would be likely to drift that much.
Remember that time() returns the server time not yours. If it returns a different value it means that the server time is different from your pc time.
This could be a caching issue compounded by a server set to another time zone—one hour ahead would seem most likely for the examples.
Turn off the browser's cache, and if the server has a cache (squid, etc.), turn that off as well, and retry the experiment.
Either that, or a server farm has a node or two with an incorrectly set time.
You can configure your server's time zone in php.ini or at runtime with the date_default_timezone_set() function.
There is possibly an error in your code. Please paste some code if you can.
Otherwise create a few test pages where you reduce the problem to a simpler code base and just test the time functions. This will help clarify and isolate any potential problems. Do you understand what I mean?
I would propose to compare script output with apache log, or even better - to also create a log which would write down the time value when it's requested. This is to exclude the following possibility:
Some of the providers have caching networks that do not deliver the content directly from you to the site, but cache it instead, in hope somebody else would also ask for the same page and then they can save outside bandwidth. So you might be getting a wrong version of the page if there's some glitch in this network.
Comparing with apache log or syslog would help you see if other code has the same idea of the time as your code - which eliminates the possibility of it being code problem and probably takes it out of the php realm into the server realm. If it is so, I would look closely into ntp - it's the only daemon I know that is supposed to mess with time.
The method is to get the time of your server by PHP and inject into client side javascript, converting it to GMT time and adding the offset thus transforming the time to that of your time zone. See this for more info.
A rogue node with an incorrectly set time on a server farm could definitely do this (as suggested by @wallyk) - and this is much more plausible than this being an NTP issue.
See if there is any way of identifying which node your request is serviced by.
Try including php_uname() or gethostname() in an HTML comment and examining the source of the page where the time is incorrect. These might give enough information to determine if the problem is associated with a specific node.
Or create a page which prints the results of the function call along with the current time() and write a script which gets that page multiple times until an anomaly is observed. (Obviously you would be careful to avoid the page being cached, and would first assure yourself that php_uname() or gethostname() gave enough information to determine whether different requests were being handled by different nodes)
精彩评论