note 78167 deleted from function.strtotime by derick

From: Date: Sun, 14 Dec 2014 11:40:14 +0000
Subject: note 78167 deleted from function.strtotime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-201186@lists.php.net to get a copy of this message
Note Submitter: Beat ---- Some surprisingly wrong results (php 5.2.0): date and time seem not coherent: <?php // Date: Default timezone Europe/Berlin (which is CET) // date.timezone no value $basedate = strtotime("31 Dec 2007 23:59:59"); $date1 = strtotime("-3 months", $basedate); echo date("j M Y H:i:s", $date1); // 1 Oct 2007 23:59:59 WRONG $basedate = strtotime("31 Dec 2007 23:59:59 CET"); $date1 = strtotime("-3 months", $basedate); echo date("j M Y H:i:s", $date1); // 1 Oct 2007 23:59:59 WRONG $basedate = strtotime("31 Dec 2007 23:59:59 GMT"); $date1 = strtotime("-3 months", $basedate); echo date("j M Y H:i:s", $date1); // 1 Oct 2007 00:59:59 CORRECT $basedate = strtotime("31 Dec 2007 22:59:59 GMT"); $date1 = strtotime("-3 months", $basedate); echo date("j M Y H:i:s", $date1); // 1 Oct 2007 23:59:59 WRONG AGAIN $basedate = strtotime("31 Dec 2007 00:00:00 GMT"); $date1 = strtotime("-3 months", $basedate); echo date("j M Y H:i:s", $date1); // 1 Oct 2007 01:00:00 CORRECT $basedate = strtotime("31 Dec 2007 00:00:00 CET"); $date1 = strtotime("-3 months", $basedate); echo date("j M Y H:i:s", $date1); // 1 Oct 2007 00:00:00 WRONG AGAIN $basedate = strtotime("31 Dec 2007 00:00:01"); $date1 = strtotime("-3 months", $basedate); echo date("j M Y H:i:s", $date1); // 1 Oct 2007 00:00:01 WRONG AGAIN ?> Here the workaround using mktime() properties on dates instead of strtotime(), and which seems to give correct results: <?php // check for equivalency $basedate = strtotime("31 Dec 2007 23:59:59"); $timedate = mktime( 23, 59, 59, 1, 0, 2008 ); echo "$basedate $timedate "; // 1199141999 1199141999 : SO THEY ARE EQUIVALENT // workaround, as mktime knows to handle properly offseted dates: $date1 = mktime( 23, 59, 59, 1 - 3, 0, 2008 ); echo date("j M Y H:i:s", $date1); // 30 Sep 2007 23:59:59 CORRECT ?>

« previous php.notes (#201186) next »