Re: strtotime bad logic or strtotime bug?

From: Date: Mon, 17 Nov 2003 18:43:49 +0000
Subject: Re: strtotime bad logic or strtotime bug?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-169990@lists.php.net to get a copy of this message
WOW! Blindingly simple mistake --> couldn't see the forest before the trees (or something like that). THANKS for the helping hand! Gnik --- Eugene Lee <list-php-1@fsck.net> wrote: > On Fri, Nov 14, 2003 at 08:53:41PM -0800, Gnik wrote: > : > : One of my servers required a PHP upgrade. Afterwards one of the PHP > : projects stopped functioning. When it would run one section would > : scroll endlessly. I can't figure out if it's a 'bug' or if it's bad > : logic in the coding. > > Bad logic. But it took me a while to figure it out. I'm glad I did; > I know it will save me countless headaches in the future! :-) > > : I isolated the problem to be in the 'strtotime' function. Here is a > : test I ran - when it runs, after getting to 2003-10-26, it scrolls > : incessently: > : > : ----BEGIN CODE------ > : <?php > : echo "<html><head><title>Testing strtotime > </title></head>"; > : print " <BR>" ; > : $stop= "2003-12-27" ; > : $start= "2003-01-01" ; > : While ($start <> $stop) { > : echo "<BR> Date: " . $start ; > : $start = date('Y-m-d', strtotime($start) + 86400); > : } > : ?> > : ----END CODE------ > > It's a logic bug due to the wonderful world of Daylight Saving Time. > When the clock strikes 2003-10-26, adding 86400 seconds to calculate the > next day's timestamp is normally correct. However, that date is also > the last Sunday of Octobor when the U.S. officially reverts from > Daylight Saving Time back to normal time. Your next day's timestamp > "loses" one hour, i.e. 3600 seconds; your timestamp for 2003-10-27 12am > is now 2003-10-26 11pm. Because your date() call extracts only the > date, > your truncation error is causing you to get stuck on 2003-10-26. This > is > the cause of your infinite loop. > > There are several ways to avoid this problem. The easiest way to change > date() to gmdate(). This gets you on UTC time, which is independent of > Daylight Saving Time or your timezone. > > Hope this helps!

« previous php.general (#169990) next »