note 78167 modified in function.strtotime by danbrown

From: Date: Sun, 09 Jan 2011 17:03:39 +0000
Subject: note 78167 modified in function.strtotime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-175577@lists.php.net to get a copy of this message
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 ?> --was-- 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 ?> http://php.net/manual/en/function.strtotime.php

« previous php.notes (#175577) next »