note 7922 deleted from function.mktime by philip

From: Date: Fri, 18 Jan 2002 17:15:35 +0000
Subject: note 7922 deleted from function.mktime by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-25049@lists.php.net to get a copy of this message
I found a curious LITTLE @#!$%^ of a problem with PHP's mktime() and date() functions. Assumptions: It is 7:08:40pm, of August 20, 2000 somewhere in the world. Wherever that is, we know that it is +4 hours to GMT. time() = 966798520 (current timestamp of 7:08:40pm of August 20, 2000) mktime(0,0,0,8,20,2000) = 966744000 (timestamp of 12:00:00am of August 20, 2000) The manual says that "time() ... Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)". The manual says that "mktime() ... Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970) and the time specified". We have the timestamp of the beginning of August 20, 2000 (966744000). We have the current time (7:08:40pm of August 20, 2000). We have the timestamp of the current time according to time() (966798520). To verify that 966744000 plus 19 hours 8 minutes and 40 seconds is equal to 966798520: First, we convert the hours and minutes into seconds and add them all up: 19 hours * 60 minutes/hour * 60 seconds/minute = 68400 8 mins * 60 seconds/minute = 480 40 secs = 40 ------ 68920 seconds Our final EQUATION becomes: 966744000 + 68920 == 966798520 Horror of horrors, it fails because: 966812920 != 966798520 The culprit? 966798520 - 966812920 = 14400 What's this? 14,400 seconds turns out to be 4 hours! HOSED! Here's the PHP code: //get the current timestamp according to time $time_ts = time(); //store the hours minutes and seconds $timestring = gmdate("G:i:s",$time_ts); //this shows current date and time according to your server's location in the world echo "current date and time: ".gmdate("n j Y G:i:s",$time_ts)."\n"; $mktime_ts = mktime(0,0,0,date("n"),date("j"),date("Y")); echo "timestamp generated by time(): $time_ts\n"; echo "timestamp generated by mktime(): $mktime_ts\n"; list($hours,$mins,$secs) = explode(":",$timestring); $elapsed_time = ($hours * 60 * 60) + ($mins * 60) + $secs; $should_be_equal_to_time_ts = $mktime_ts + $elapsed_time; if ($should_be_equal_to_time_ts != $time_ts) { echo "Uh-oh! $should_be_equal_to_time_ts is different from $time_ts!\n".($time_ts - $should_be_equal_to_time_ts)." seconds difference!\n"; } else echo "$should_be_equal_to_time_ts is the same as $time_ts";

« previous php.notes (#25049) next »