php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53539 strtotime off-by-1-week bug still not fixed
Submitted: 2010-12-13 16:59 UTC Modified: 2012-11-19 02:55 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: kugel at rci dot rutgers dot edu Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.3SVN-2010-12-13 (SVN) OS: Fedora
Private report: No CVE-ID: None
 [2010-12-13 16:59 UTC] kugel at rci dot rutgers dot edu
Description:
------------
---
From manual page: http://www.php.net/function.strtotime#Changelog
---
"5.2.7	In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions."

Contrary to the above, this bug still exists in PHP 5.2.13, 5.2.14, and 5.3.3. 

Test script:
---------------
<?php
date_default_timezone_set("America/New_York");

echo date("D, M j Y", strtotime("first Saturday January 2011 8:00pm")) . "\n";
echo date("D, M j Y", strtotime("third Saturday January 2011 8:00pm")) . "\n";
?>


Expected result:
----------------
Sat, Jan 1 2011
Sat, Jan 15 2011

Actual result:
--------------
Sat, Jan 8 2011
Sat, Jan 22 2011

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-13 17:01 UTC] kugel at rci dot rutgers dot edu
-Summary: Bug still not fixed +Summary: strtotime off-by-1-week bug still not fixed
 [2010-12-13 17:01 UTC] kugel at rci dot rutgers dot edu
better bug title
 [2010-12-13 17:29 UTC] derick@php.net
-Package: *General Issues +Package: Documentation problem
 [2010-12-13 17:29 UTC] derick@php.net
This is not a *code* bug though.

It works like follows:
"January 2011 8:00pm" is an absolute date string, which processed first:

$ php -r 'echo date("Y-m-d H:i:s", strtotime("January 2011 8:00pm"));'
2011-01-01 20:00:00
$ php -r 'echo strtotime("January 2011 8:00pm");'
1293912000

"first Saturday" is a string relative to the absolute date string, which is processed next:
$ php -r 'echo date("Y-m-d H:i:s", strtotime("first Saturday", 1293912000));'
2011-01-08 00:00:00

You are missing the "of" keyword, which links the two together:

$ php -r 'echo date("Y-m-d H:i:s", strtotime("first Saturday of January 2011 8:00pm")), "\n";'
2011-01-01 20:00:00

$ php -r 'echo date("Y-m-d H:i:s", strtotime("third Saturday of January 2011 8:00pm")), "\n";'
2011-01-15 20:00:00
 [2010-12-13 19:13 UTC] kugel at rci dot rutgers dot edu
The corrected datestring works on my personal 5.3.3 machine, but not on my dreamhost (PHP 5.2.14) host; that shows as "Wed, Dec 31 1969".  I did find a note in the datetime documentation suggesting relative formats were broken before 5.3, so I think everything is now explained or noted.
 [2012-11-19 02:55 UTC] levim@php.net
-Status: Open +Status: Not a bug
 [2012-11-19 02:55 UTC] levim@php.net
As noted by derick, you need to use the "of" keyword to link the relative and 
absolute formats.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Jan 17 03:01:23 2021 UTC