Bluehost.com Web Hosting $6.95

strtotime bad logic or strtotime bug?

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-15-2003
Gnik
 
Posts: n/a
Default strtotime bad logic or strtotime bug?

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
Reply With Quote
  #2 (permalink)  
Old 11-15-2003
Chris Shiflett
 
Posts: n/a
Default Re: [PHP] strtotime bad logic or strtotime bug?

--- 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
Reply With Quote
  #3 (permalink)  
Old 11-17-2003
Eugene Lee
 
Posts: n/a
Default Re: [PHP] strtotime bad logic or strtotime bug?

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!
Reply With Quote
  #4 (permalink)  
Old 11-17-2003
Gnik
 
Posts: n/a
Default Re: [PHP] strtotime bad logic or strtotime bug?

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!

Reply With Quote
  #5 (permalink)  
Old 01-19-2004
Philippe Guiol
 
Posts: n/a
Default Re: strtotime bad logic or strtotime bug?

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
##-----------------------------------------------##
Reply With Quote
  #6 (permalink)  
Old 12-04-2004
Christian Zambrano
 
Posts: n/a
Default Re: strtotime bad logic or strtotime bug?

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
##-----------------------------------------------##
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 05:41 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0