From: [EMAIL PROTECTED] Operating system: Linux Mandrake 7.2 PHP version: 4.2.3 PHP Bug Type: Date/time related Bug description: strtotime ("Monday", $date) can produce incorrect output
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: ======================================================= [root@www 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> [root@www 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 bug report at http://bugs.php.net/?id=20382&edit=1 -- Try a CVS snapshot: http://bugs.php.net/fix.php?id=20382&r=trysnapshot Fixed in CVS: http://bugs.php.net/fix.php?id=20382&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=20382&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=20382&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=20382&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=20382&r=support Expected behavior: http://bugs.php.net/fix.php?id=20382&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=20382&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=20382&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=20382&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=20382&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=20382&r=dst IIS Stability: http://bugs.php.net/fix.php?id=20382&r=isapi