note 47079 deleted from function.mktime by danbrown

From: Date: Sun, 20 Jul 2008 03:37:24 +0000
Subject: note 47079 deleted from function.mktime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142081@lists.php.net to get a copy of this message
Note Submitter: phpmanual at localpin dot com ---- Daylight saving time! Grrrrrrrrrr! (And a minor "Grrr!" for PHP making this less intuitive than it should be). In brief, NEVER add number of seconds, like 60*60*24, but use strtotime("+1 day", $myDate)). Some of the 'simple' functions above for calculating dates will NOT work correctly when a date includes daylight saving time. Shown below is the problem illustrated and a simple solution. EXAMPLE 1: WRONG! I had the following loop, which worked fine all year, until this weekend, when the clocks changed, and it broke! The problem is daylight saving time: // Loop through dates adding 1 days worth of seconds each time $myDate = 1098914400 // October 28th for ($myDate = $fromDate; $myDate <= $toDate; $myDate = $myDate + 60 * 60 * 24) { $printable = strftime("%d-%m-%Y %H:%M:%S", $myDate); echo $printable } "28-10-2004 00:00:00" "29-10-2004 00:00:00" "30-10-2004 00:00:00" "31-10-2004 00:00:00" "31-10-2004 23:00:00" (oops! 31st October repeats and time now 23:00!!) "01-11-2004 23:00:00" "02-11-2004 23:00:00" EXAMPLE 2: CORRECT! $myDate = 1098914400 // October 28th for ($myDate = $fromDate; $myDate <= $toDate; $myDate = strtotime("+1 day", $myDate)) { $printable = strftime("%d-%m-%Y %H:%M:%S", $myDate); echo $printable } "28-10-2004 00:00:00" "29-10-2004 00:00:00" "30-10-2004 00:00:00" "31-10-2004 00:00:00" "01-11-2004 00:00:00" "02-11-2004 00:00:00"

« previous php.notes (#142081) next »