Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strtotime() does not return the correct date

Status
Not open for further replies.

mkrausnick

Programmer
Apr 2, 2002
766
US
I am new to PHP. The following test script does not seem to produce the correct results on my server. The server is located in the U.S. eastern time zone, if that makes any difference.

The script is:
Code:
<?php 
echo 'today is ';
echo date(Ymd);
echo '<br>sunday is ';
echo date(Ymd,strtotime('sunday'));
echo '<br>this week sunday is ';
echo date(Ymd,strtotime('this week sunday'));
echo '<br>next week sunday is ';
echo date(Ymd,strtotime('next week sunday'));
echo '<br>last week sunday is ';
echo date(Ymd,strtotime('last week sunday'));
echo '<br><br>' ;
echo date(Ymd);
echo '<br>monday is ';
echo date(Ymd,strtotime('monday'));
echo '<br>this week monday is ';
echo date(Ymd,strtotime('this week monday'));
echo '<br>next week monday is ';
echo date(Ymd,strtotime('next week monday'));
echo '<br>last week monday is ';
echo date(Ymd,strtotime('last week monday'));
echo '<br><br>' ;
echo date(Ymd);
echo '<br>tuesday is ';
echo date(Ymd,strtotime('tuesday'));
echo '<br>this week tuesday is ';
echo date(Ymd,strtotime('this week tuesday'));
echo '<br>next week tuesday is ';
echo date(Ymd,strtotime('next week tuesday'));
echo '<br>last week tuesday is ';
echo date(Ymd,strtotime('last week tuesday'));
echo '<br><br>' ;
echo '<br>wednesday is ';
echo date(Ymd,strtotime('wednesday'));
echo '<br>this week wednesday is ';
echo date(Ymd,strtotime('this week wednesday'));
echo '<br>next week wednesday is ';
echo date(Ymd,strtotime('next week wednesday'));
echo '<br>last week wednesday is ';
echo date(Ymd,strtotime('last week wednesday'));
echo '<br><br>' ;
echo date(Ymd);
echo '<br>thursday is ';
echo date(Ymd,strtotime('thursday'));
echo '<br>this week thursday is ';
echo date(Ymd,strtotime('this week thursday'));
echo '<br>next week thursday is ';
echo date(Ymd,strtotime('next week thursday'));
echo '<br>last week thursday is ';
echo date(Ymd,strtotime('last week thursday'));
echo '<br><br>' ;
echo date(Ymd);
echo '<br>friday is ';
echo date(Ymd,strtotime('friday'));
echo '<br>this week friday is ';
echo date(Ymd,strtotime('this week friday'));
echo '<br>next week friday is ';
echo date(Ymd,strtotime('next week friday'));
echo '<br>last week friday is ';
echo date(Ymd,strtotime('last week friday'));
echo '<br><br>' ;
echo date(Ymd);
echo '<br>saturday is ';
echo date(Ymd,strtotime('saturday'));
echo '<br>this week saturday is ';
echo date(Ymd,strtotime('this week saturday'));
echo '<br>next week saturday is ';
echo date(Ymd,strtotime('next week saturday'));
echo '<br>last week saturday is ';
echo date(Ymd,strtotime('last week saturday'));
?>

After accessing the page on Friday, October 24th 2003, the result I see in my browser is:


Test Page today is 20031024
sunday is 20031026
this week sunday is 20031102
next week sunday is 20031109
last week sunday is 20031019

20031024
monday is 20031026
this week monday is 20031103
next week monday is 20031110
last week monday is 20031020

20031024
tuesday is 20031027
this week tuesday is 20031104
next week tuesday is 20031111
last week tuesday is 20031021


wednesday is 20031028
this week wednesday is 20031105
next week wednesday is 20031112
last week wednesday is 20031022

20031024
thursday is 20031029
this week thursday is 20031106
next week thursday is 20031113
last week thursday is 20031023

20031024
friday is 20031024
this week friday is 20031031
next week friday is 20031107
last week friday is 20031017

20031024
saturday is 20031025
this week saturday is 20031101
next week saturday is 20031108
last week saturday is 20031018


Note that all dates from 'sunday' through 'thursday' are off by one day. In fact, as I've been checking this page all week, the result is that the date is off by one day for each day up to the current day, then from the current day forward, the date is correct.

Is this a known issue with PHP, does my hosting service not have the current version of PHP, or what?



Mike Krausnick
Dublin, California
 
You've found a known bug that's recorded here:
The problem seems to be that when using relative date string (like just day-names) and that date is on the other side of the DST/non-DST boundary. My own testing seems to support this.

I'm using the following test script (pulled from parts of yours):

<?php
echo 'today is ';
echo date('Ymd', strtotime('today'));

echo '<br><br>friday is ';
echo date(Ymd,strtotime('friday'));
echo '<br>saturday is ';
echo date(Ymd,strtotime('saturday'));
echo '<br>sunday is ';
echo date('Ymd',strtotime('sunday'));
echo '<br>monday is ';
echo date('Ymd',strtotime('Monday'));
echo '<br>tuesday is ';
echo date(Ymd,strtotime('tuesday'));
echo '<br>wednesday is ';
echo date(Ymd,strtotime('wednesday'));
echo '<br>thursday is ';
echo date(Ymd,strtotime('thursday'));
?>

With my system's date set to today (Friday, 2003-10-24), I get the following output:

today is 20031024 (correct)
friday is 20031024 (correct. today is Friday)
saturday is 20031025 (correct)
sunday is 20031026 (correct)
monday is 20031026 (incorrect)
tuesday is 20031027 (incorrect)
wednesday is 20031028 (incorrect)
thursday is 20031029 (incorrect)

If I change my system's date to 2003-10-17 (last Friday) and rerun the script, I get:

today is 20031017 (correct according to the system clock)
friday is 20031017 (correct)
saturday is 20031018 (correct)
sunday is 20031019 (correct)
monday is 20031020 (correct)
tuesday is 20031021 (correct)
wednesday is 20031022 (correct)
thursday is 20031023 (correct)


If I change the date to 2003-10-31, I get:

today is 20031031 (correct according to the system clock)
friday is 20031031 (correct)
saturday is 20031101 (correct)
sunday is 20031102 (correct)
monday is 20031103 (correct)
tuesday is 20031104 (correct)
wednesday is 20031105 (correct)
thursday is 20031106 (correct)

On the 17th and 31st, these relative dates do not cross the DST/non-DST boundary.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top