This is a discussion on strtotime bad logic or strtotime bug? within the PHP General forums, part of the PHP Programming Forums category; Greetings, One of my servers required a PHP upgrade. Afterwards one of the PHP projects stopped functioning. When it would ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Greetings,
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. 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------ Is there something wrong with the logic/code or is it a bug? I'm using strtotime mainly for decoding a MySQL date field (iterating thru all date records and generating HTML forms). PHP: 4.3.4 / Apache 2.0.48 Thanks for your time, YAPHPN [Yet.Another.PHPNewbie] Gnik |
|
|||
|
--- Gnik <gniknalu@sbcglobal.net> wrote:
> 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 > $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------ Your approach makes me wonder why you are bothering to convert between timestamps and formatted dates so many times. It makes much more sense to use timestamps for conditionals and math, and use date() only in your echo statement. As for your particular error, I'm sure you could easily find it with just a few minutes of debugging. For example, output the value of $start and $stop at the beginning and very end of each loop. Also, you might want to use != for not equal. From the manual (http://www.php.net/manual/en/language.expressions.php): PHP supports > (bigger than), >= (bigger than or equal to), == (equal), != (not equal), < (smaller than) and <= (smaller than or equal to). These expressions are most commonly used inside conditional execution, such as if statements. Hope that helps. Chris ===== Chris Shiflett - http://shiflett.org/ PHP Security Handbook Coming mid-2004 HTTP Developer's Handbook http://httphandbook.org/ RAMP Training Courses http://www.nyphp.org/ramp |
|
|||
|
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! |
|
|||
|
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! |
|
|||
|
Yeah, time and date comparison is a real hassle. I eventually handled it
through : if(strnatcascamp($value1, $value2)>-1) { then $value1 is bigger than $value2 } Enjoy Gnik wrote: > Greetings, > 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. > 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------ > Is there something wrong with the logic/code or is it a bug? I'm using > strtotime mainly for decoding a MySQL date field (iterating thru all > date > records and generating HTML forms). > PHP: 4.3.4 / Apache 2.0.48 > Thanks for your time, > YAPHPN [Yet.Another.PHPNewbie] > Gnik ##-----------------------------------------------# Article posted from PHP Freaks NewsGroup http://www.phpfreaks.com/newsgroup Get Addicted: php.genera ##-----------------------------------------------## |
|
|||
|
It is a pretty interesting problem. You shouldn't try to do arithmetic on
timestamps. Replace this code with yours and you shouldn't have aproblem. <?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("+1 day",strtotime($start))); } ?> Gnik wrote: > Greetings, > 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. > 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------ > Is there something wrong with the logic/code or is it a bug? I'm using > strtotime mainly for decoding a MySQL date field (iterating thru all > date > records and generating HTML forms). > PHP: 4.3.4 / Apache 2.0.48 > Thanks for your time, > YAPHPN [Yet.Another.PHPNewbie] > Gnik ##-----------------------------------------------# Article posted from PHP Freaks NewsGroup http://www.phpfreaks.com/newsgroup Get Addicted: php.genera ##-----------------------------------------------## |