ID:               20382
 Updated by:       [EMAIL PROTECTED]
 Reported By:      nickj-phpbugs at nickj dot org
-Status:           Verified
+Status:           Closed
 Bug Type:         Date/time related
 Operating System: *
 PHP Version:      4CVS, 5CVS
 Assigned To:      hholzgra
 New Comment:

This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.




Previous Comments:
------------------------------------------------------------------------

[2003-04-06 18:47:03] scottmacvicar at ntlworld dot com

This is a DST problem by the looks of it. The specific dates mentioned
are when DST takes effect which is the last sunday in March.

------------------------------------------------------------------------

[2002-11-12 19:46:28] nickj-phpbugs at nickj dot org

OK, I decided that was needed was some kind of automated testing, so I
wrote it:

============================================

#!/root/php4-200211122230 -q

<?php



// report any errors at all

error_reporting (E_ALL);



// pass a date, supply a strtotime modifier, and get a date back

function getDateWithModifier($date, $modifier) {

        list ($year, $month, $day) = explode ("-",$date);

        $starting_timestamp = mktime (1,1,1,$month,$day,$year);

        $timestamp_with_modifier = strtotime ($modifier,
$starting_timestamp);

        return date("Y-n-j", $timestamp_with_modifier);

}



/*

** @desc: for the specified date, will find the date for the desired
day of the 

**        week that is also in the same week. Does NOT use 'strtotime'

*/

function getDayOfTheWeekFromDate($date, $desired_day_of_week) {

        // weekdays - note special case for sundays (7, not 0), so as to treat
as end of week, not start

    $weekdays = array ("Sunday" => 7, "Monday" => 1, "Tuesday" => 2,
"Wednesday" => 3,

                            "Thursday" => 4, "Friday" => 5, "Saturday"
=> 6);



    // convert into a number

    $desired_day_of_week_number = $weekdays[$desired_day_of_week];



    // see what day we have currently

    list ($year, $month, $day) = explode ("-",$date);

    $date_day_of_week = date("w", mktime
(17,17,17,$month,$day,$year));



        $new_day = $day+(($desired_day_of_week_number-$date_day_of_week)+7) %
7;

    return date("Y-n-j", mktime (17,17,17,$month,$new_day,$year));

}





// run an automated test to compare the output of these two functions,
and complain if they differ

for ($i=1; $i<1000; $i++) {

        $date =  date("Y-n-j", mktime (17,17,17,1,$i,1999));

        $strtotime_date = getDateWithModifier($date, "Monday");

        $other_date = getDayOfTheWeekFromDate($date, "Monday");

        if ($strtotime_date != $other_date) {

                print "Discrepancy for $date - results were $strtotime_date vs
$other_date\n";

        }

}



print "PHP version: " . phpversion(). "<br>\n";



?>

============================================





Here's the output that I get:

============================================

[EMAIL PROTECTED] tmp]# ./automated-date-test.php 

Discrepancy for 1999-3-23 - results were 1999-3-28 vs 1999-3-29

Discrepancy for 1999-3-24 - results were 1999-3-28 vs 1999-3-29

Discrepancy for 1999-3-25 - results were 1999-3-28 vs 1999-3-29

Discrepancy for 1999-3-26 - results were 1999-3-28 vs 1999-3-29

Discrepancy for 1999-3-27 - results were 1999-3-28 vs 1999-3-29

Discrepancy for 1999-3-28 - results were 1999-3-28 vs 1999-3-29

Discrepancy for 2000-3-21 - results were 2000-3-26 vs 2000-3-27

Discrepancy for 2000-3-22 - results were 2000-3-26 vs 2000-3-27

Discrepancy for 2000-3-23 - results were 2000-3-26 vs 2000-3-27

Discrepancy for 2000-3-24 - results were 2000-3-26 vs 2000-3-27

Discrepancy for 2000-3-25 - results were 2000-3-26 vs 2000-3-27

Discrepancy for 2000-3-26 - results were 2000-3-26 vs 2000-3-27

Discrepancy for 2001-3-20 - results were 2001-3-25 vs 2001-3-26

Discrepancy for 2001-3-21 - results were 2001-3-25 vs 2001-3-26

Discrepancy for 2001-3-22 - results were 2001-3-25 vs 2001-3-26

Discrepancy for 2001-3-23 - results were 2001-3-25 vs 2001-3-26

Discrepancy for 2001-3-24 - results were 2001-3-25 vs 2001-3-26

Discrepancy for 2001-3-25 - results were 2001-3-25 vs 2001-3-26

PHP version: 4.3.0-dev<br>

[EMAIL PROTECTED] tmp]# 

============================================



In other words, the result for these 6 days of the year consistently
appears to be wrong. (I suppose I should be glad that the very first
date I choose to test this function with just by fluke happened to be
one of those 6 days, as opposed to causing mysterious problems later!)




Does anyone else get any results that appear incorrect on running this
script?

------------------------------------------------------------------------

[2002-11-12 02:11:16] nickj-phpbugs at nickj dot org

Strtotime() appears to produce incorrect output under some
circumstances - specifically when wanting the date of a particular day
of the week day of the week from a given starting date. For example,
using 2001-3-20 as a starting point, we want the date for the next
Monday. The correct answer is the 26th, but the result produced is the
25th (a Sunday). The correct result is given sometimes, so this appears
to depend on the input.



A sample script to illustrate:

=======================================================

#!/root/php-4.2.3 -q

<?php



// report any errors at all

error_reporting (E_ALL);



// pass a date, supply a strtotime modifier, and get a date back

function getDateWithModifier($date, $modifier) {

        list ($year, $month, $day) = explode ("-",$date);

        $starting_timestamp = mktime (10,10,10,$month,$day,$year);

        $timestamp_with_modifier = strtotime ($modifier,
$starting_timestamp);

        return date("Y-n-j", $timestamp_with_modifier);

}



print "<hr>\n";

print "from 2001-3-17, goto monday: " .
getDateWithModifier("2001-3-17", "Monday") . "; should be
2001-3-19.<br>\n";

print "from 2001-3-18, goto monday: " .
getDateWithModifier("2001-3-18", "Monday") . "; should be
2001-3-19.<br>\n";

print "from 2001-3-19, goto monday: " .
getDateWithModifier("2001-3-19", "Monday") . "; should be
2001-3-19.<br>\n";

print "from 2001-3-20, goto monday: " .
getDateWithModifier("2001-3-20", "Monday") . "; should be 2001-3-26
###.<br>\n";

print "from 2001-3-21, goto monday: " .
getDateWithModifier("2001-3-21", "Monday") . "; should be 2001-3-26
###.<br>\n";

print "from 2001-3-22, goto monday: " .
getDateWithModifier("2001-3-22", "Monday") . "; should be 2001-3-26
###.<br>\n";

print "from 2001-3-23, goto monday: " .
getDateWithModifier("2001-3-23", "Monday") . "; should be 2001-3-26
###.<br>\n";

print "from 2001-3-24, goto monday: " .
getDateWithModifier("2001-3-24", "Monday") . "; should be 2001-3-26
###.<br>\n";

print "from 2001-3-25, goto monday: " .
getDateWithModifier("2001-3-25", "Monday") . "; should be 2001-3-26
###.<br>\n";

print "from 2001-3-26, goto monday: " .
getDateWithModifier("2001-3-26", "Monday") . "; should be
2001-3-26.<br>\n";

print "from 2001-3-27, goto monday: " .
getDateWithModifier("2001-3-27", "Monday") . " ; should be
2001-4-2.<br>\n";

print "<hr>\n";



print "PHP version: " . phpversion(). "<br>\n";



?>

=======================================================



Produces this output:

=======================================================

[EMAIL PROTECTED] tmp]# ./script.php 

<hr>

from 2001-3-17, goto monday: 2001-3-19; should be 2001-3-19.<br>

from 2001-3-18, goto monday: 2001-3-19; should be 2001-3-19.<br>

from 2001-3-19, goto monday: 2001-3-19; should be 2001-3-19.<br>

from 2001-3-20, goto monday: 2001-3-25; should be 2001-3-26 ###.<br>

from 2001-3-21, goto monday: 2001-3-25; should be 2001-3-26 ###.<br>

from 2001-3-22, goto monday: 2001-3-25; should be 2001-3-26 ###.<br>

from 2001-3-23, goto monday: 2001-3-25; should be 2001-3-26 ###.<br>

from 2001-3-24, goto monday: 2001-3-25; should be 2001-3-26 ###.<br>

from 2001-3-25, goto monday: 2001-3-25; should be 2001-3-26 ###.<br>

from 2001-3-26, goto monday: 2001-3-26; should be 2001-3-26.<br>

from 2001-3-27, goto monday: 2001-4-2 ; should be 2001-4-2.<br>

<hr>

PHP version: 4.2.3<br>

[EMAIL PROTECTED] tmp]#

========================================================



Expected output: The 6 lines with "###" are expected to produce the
indicated output, but do not.



Problem was noticed today in PHP 4.06, and then verified as being
reproducible in PHP 4.2.3 (same output).



PHP 4.2.3 configured with:

./configure --with-mysql --enable-xml --enable-wddx 



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=20382&edit=1

Reply via email to