Tuesday, March 1, 2011

Why is strtotime($lastmoment) bigger than time()?

The following code

 echo $lastmoment."<br/>";
echo time();
echo "<br/>";
echo strtotime($lastmoment);

outputs:

2009-12-15 17:40:53 1260876829 1260898853

What's wrong? $lastment is a past time stamp in MySQL, why is strtotime($lastmoment) bigger than time()?

From stackoverflow
  • Because the time() function was may be executed some milliseconds before the $lastmoment.

    EDIT: Adding the comment to my thoughts, it might me that, the convert of strtotime() might have failed. Or the date value from the database contains a value e.g. days which the date() does not have. Be sure, that you compare 2 datevalues of the same format.

    Steven : $lastmoment was fetched from database, which was a past time stamp.
    Steven : $lastmoment's value is fixed.
    Pekka : Can you show $lastmoment?
    Anthony : If strtotime failed, it should throw a -1, and thus Dec 31st 1969. And even if the date value from the DB was more precise, etc, the whole point of strtotime is to convert any date/time to epoch. So even if it was to the millisecond, it would round off to epoch.
  • If your MySQL server is a different machine, or if the timestamp in $lastmoment was set from a different machine, you could be seeing clock drift. Check the system clocks on the various machines, and see if they agree.

    Anthony : Rats! +1 for figuring that out right before I did!

0 comments:

Post a Comment