php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63227 strtotime - weekday in a month where that weekday was the first day adds 1 week
Submitted: 2012-10-05 19:04 UTC Modified: 2022-05-13 13:47 UTC
From: helgeson dot b at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.0.1 OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: helgeson dot b at gmail dot com
New email:
PHP Version: OS:

 

 [2012-10-05 19:04 UTC] helgeson dot b at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.strtotime#refsect1-
function.strtotime-changelog
---

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.  Although this was reported resolved as of PHP 5.2.7, it still exists 
in PHP 5.3.3.

Maybe update the documentation to reflect the version the bug was actually 
corrected in?



Test script:
---------------
$x = strtotime('Second Monday October 2012');

print date('Y-m-d', $x);


Expected result:
----------------
2012-10-8

Actual result:
--------------
2012-10-15

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-05 19:06 UTC] helgeson dot b at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.strtotime#refsect1-
function.strtotime-changelog
---

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.  Although this was reported resolved as of PHP 5.2.7, it still exists 
in PHP 5.3.17.
 [2015-12-25 12:36 UTC] cmb@php.net
-Status: Open +Status: Verified -Operating System: CentOS 6.2 +Operating System: * -PHP Version: 5.3.17 +PHP Version: 7.0.1
 [2015-12-25 12:36 UTC] cmb@php.net
> Maybe update the documentation to reflect the version the bug
> was actually corrected in?

Bug #43452 has been fixed as of PHP 5.2.7[1], but apparently the
fix only affects week numbers given as proper number[2], but not
as ordinal string[3].

[1] <https://3v4l.org/I4HLT>
[2] <https://3v4l.org/GUdnb>
[3] <https://3v4l.org/LhGLZ>
 [2016-02-13 13:43 UTC] murden at gmail dot com
I've found something that seems to be related? running strtotime on a string like ("2nd monday") adds a week to the date on months where the requested week day is the 1st day of the month. Like, will also happen on ("1st Saturday") when the month starts on a Saturday.
 [2022-05-13 13:47 UTC] derick@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: derick
 [2022-05-13 13:47 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

This is expected, and documented in the second note at https://www.php.net/manual/en/datetime.formats.relative.php:

2. "number dayname" does not advance to another day. (Example: "1 wednesday july 23rd, 2008" means "2008-07-23"). 
4.  "ordinal dayname" does advance to another day. (Example "first wednesday july 23rd, 2008" means "2008-07-30"). 

$ php

<?php
echo date('Y-m-d', strtotime("second monday october 2012")), "\n";
echo date('Y-m-d', strtotime("2 monday october 2012")), "\n";
?>

2012-10-15
2012-10-08
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 19:01:29 2024 UTC