php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27431 possible leap year strtotime hiccup
Submitted: 2004-02-28 18:50 UTC Modified: 2004-02-29 13:24 UTC
From: pettyr at hotmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 4.3.4 OS: Linux
Private report: No CVE-ID: None
 [2004-02-28 18:50 UTC] pettyr at hotmail dot com
Description:
------------
The clients server is hosted by a third party running debian SMP Apache and PHP 4.3.4.  The use of strtotime("next monday") skips monday the 1st and returns monday the 8th when run on Feb. 28th (tomorrow is a leap day.)

The servers particulars:
[dali]$ uname -a
Linux dali 2.4.24-grsec+w+fhs5+gr1913+nfs+++p3+c3+bu+gr0b-v6.182 #1 SMP Mon Jan 5 12:43:44 PST 2004 i686 unknown
[dali]$ date
Sat Feb 28 14:33:39 PST 2004

Reproduce code:
---------------
[dali]$ cat t1.php
<?
$start = strtotime('next monday');
echo ('Start timestamp: '.$start.'<br>'."\n");
$first = strtotime('first Monday',$start);
echo ('"First" Monday: '.date('l, M d Y',$first).'<br>'."\n");
$oneth = strtotime('1 Monday',$start);
echo ('"1" Monday: '.date('l, M d Y',$oneth).'<br>'."\n");
$next = strtotime('next Monday',$start);
echo ('"Next" Monday: '.date('l, M d Y',$next).'<br>'."\n");
$twoth = strtotime('2 Monday',$start);
echo ('"2" Monday: '.date('l, M d Y',$twoth).'<br>'."\n");
$third = strtotime('third Monday',$start);
echo ('"Third" Monday: '.date('l, M d Y',$third).'<br>'."\n");
$threeth = strtotime('3 Monday',$start);
echo ('"3" Monday: '.date('l, M d Y',$threeth).'<br>'."\n");
?>

Expected result:
----------------
[dali]$ php t1.php
Content-type: text/html
X-Powered-By: PHP/4.3.4

Start timestamp: 1078732800<br>
"Next" Monday: Monday, Mar 01 2004<br>
"First" Monday: Monday, Mar 01 2004<br>
"1" Monday: Monday, Mar 01 2004<br>
"Next" Monday: Monday, Mar 08 2004<br>
"2" Monday: Monday, Mar 08 2004<br>
"Third" Monday: Monday, Mar 15 2004<br>
"3" Monday: Monday, Mar 15 2004<br>
[dali]$ 



Actual result:
--------------
[dali]$ php t1.php
Content-type: text/html
X-Powered-By: PHP/4.3.4

Start timestamp: 1078732800<br>
"Next" Monday: Monday, Mar 08 2004<br>
"First" Monday: Monday, Mar 08 2004<br>
"1" Monday: Monday, Mar 08 2004<br>
"Next" Monday: Monday, Mar 15 2004<br>
"2" Monday: Monday, Mar 15 2004<br>
"Third" Monday: Monday, Mar 22 2004<br>
"3" Monday: Monday, Mar 22 2004<br>
[dali]$ 




But, the exact same code run from my system:

[root@helios tmp]# uname -a
Linux helios 2.4.20-28.7smp #1 SMP Thu Dec 18 11:18:31 EST 2003 i686 unknown
[root@helios tmp]# date
Sat Feb 28 13:34:08 MST 2004
[root@helios tmp]# php /tmp/t1.php
X-Powered-By: PHP/4.1.2
Content-type: text/html

Start timestamp: 1078124400<br>
"Next" Monday: Monday, Mar 01 2004<br>
"First" Monday: Monday, Mar 01 2004<br>
"1" Monday: Monday, Mar 01 2004<br>
"Next" Monday: Monday, Mar 01 2004<br>
"2" Monday: Monday, Mar 08 2004<br>
"Third" Monday: Monday, Mar 15 2004<br>
"3" Monday: Monday, Mar 15 2004<br>

May be related to:
Bug #18670 strtotime() bug 
Submitted: 31 Jul 2002 10:26am EDT Modified: 18 Oct 2003 10:57pm EDT 


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-29 03:43 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

On the system you say it \"works\" is PHP 4.1.2, but actually it was a bug in that version. strtotime() follows the GNU semantics (as you can read on php.net/strtotime) and they say:

(from: http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#SEC103)
The explicit mention of a day of the week will forward the date (only if necessary) to reach that day of the week in the future.

Days of the week may be spelled out in full: `Sunday\', `Monday\', `Tuesday\', `Wednesday\', `Thursday\', `Friday\' or `Saturday\'. Days may be abbreviated to their first three letters, optionally followed by a period. The special abbreviations `Tues\' for `Tuesday\', `Wednes\' for `Wednesday\' and `Thur\' or `Thurs\' for `Thursday\' are also allowed.

A number may precede a day of the week item to move forward supplementary weeks. It is best used in expression like `third monday\'. In this context, `last day\' or `next day\' is also acceptable; they move one week before or after the day that day by itself would represent. 


This means that the current behavoir is correct.
 [2004-02-29 12:00 UTC] pettyr at hotmail dot com
OK, I am stupid then... The explanation did not help clarify why this is correct behavior, given, on Saturday the 28th of Feb, 2004, "next Monday" (in the GNU/PHP world) is March 8th.  Bummer, that means that if GNU/PHP is cutting my paycheck I'd be a week short.  Maybe I should go back to perl, PHP is too 'occasionally' different but still totally correct. Well, as long as you're not running it in production it won't ever really matter.

Regards
 [2004-02-29 13:24 UTC] derick@php.net
it's easy:
"monday" or "first monday" means the first available monday from the current time.
"next monday" or "second monday" means the second available monday from the current time.

And bickering over PHP being non standard is not helping anybody. I did do some research though with the unix "date" command, and it seems that it is indeed behaving like you should say so. So I have to agree that it's a bug somewhere.

Though the GNU manual definitely states that "next" is "2". 
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Dec 04 18:00:01 2025 UTC