php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77941 strtotime "next <weekday>" are off by one for dates parsed from calendar week
Submitted: 2019-04-25 09:25 UTC Modified: 2022-07-22 16:29 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: mail at cebe dot cc Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.3.4 OS: Linux
Private report: No CVE-ID: None
 [2019-04-25 09:25 UTC] mail at cebe dot cc
Description:
------------
when working on a date parsed from calendar week from the pattern {YYYY}W{WW}, e.g. "2019W16" strtotime returns the date of Monday in the calendar week 16 in the year 2019, which is 2019-04-15.

When this is combinated with a "next <weekday>" e.g. "2019W16 next friday", the result is one off, in this case 2019-04-18, which is Thursday of that week, not Friday.

Giving "2019-04-15 next friday" gives the correct date: 2019-04-19.

According to my test script, this is broken since PHP 5.2, while working fine in 5.1: https://3v4l.org/0s4Ns

Test script:
---------------
<?php

// see also https://3v4l.org/0s4Ns

if (function_exists('date_default_timezone_set')) date_default_timezone_set("UTC");
echo "works as expected:\n";
echo date("Y-m-d H:i:s", strtotime("2019W14")) . " should be 2019-04-01\n";
echo date("Y-m-d H:i:s", strtotime("2019W15")) . " should be 2019-04-08\n";
echo date("Y-m-d H:i:s", strtotime("2019W16")) . " should be 2019-04-15\n\n";

echo "off by one:\n";
echo date("Y-m-d H:i:s", strtotime("2019W16 next tuesday")) . " should be 2019-04-16\n";
echo date("Y-m-d H:i:s", strtotime("2019W16 next wednesday")) . " should be 2019-04-17\n";
echo date("Y-m-d H:i:s", strtotime("2019W16 next thursday")) . " should be 2019-04-18\n";
echo date("Y-m-d H:i:s", strtotime("2019W16 next friday")) . " should be 2019-04-19\n";
echo date("Y-m-d H:i:s", strtotime("2019W16 next saturday")) . " should be 2019-04-20\n";
echo date("Y-m-d H:i:s", strtotime("2019W16 next sunday")) . " should be 2019-04-21\n";
echo date("Y-m-d H:i:s", strtotime("2019W16 next monday")) . " should be 2019-04-22\n\n";

echo "works as expected:\n";
echo date("Y-m-d H:i:s", strtotime("2019-04-15 next monday")) . " should be 2019-04-22\n";
echo date("Y-m-d H:i:s", strtotime("2019-04-15 next friday")) . " should be 2019-04-19\n";
echo date("Y-m-d H:i:s", strtotime("2019-04-15 next thursday")) . " should be 2019-04-18\n";

Expected result:
----------------
see above.

Actual result:
--------------
see above.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-25 13:21 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-04-25 13:21 UTC] cmb@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

You have to use 'o' in combination with 'W'.
 [2019-04-25 21:04 UTC] a at b dot c dot de
How would using the ISO year make a difference in the middle of April? And why would it cause a shift of one day?

Consider
echo date("o-W-N", strtotime("2019W16 next tuesday"));

Between 5.1.6 and 5.2.0 the output changes from Tuesday of Week 16 (2019-16-2) to Monday of Week 17 (2019-17-1).

Tuesday isn't Monday even in the ISO calendar.
 [2019-04-25 21:17 UTC] cmb@php.net
-Status: Not a bug +Status: Re-Opened
 [2019-04-25 21:17 UTC] cmb@php.net
Seems I closed this too quick – sorry.  Re-opening.
 [2019-04-26 11:21 UTC] cmb@php.net
-Assigned To: cmb +Assigned To:
 [2022-07-22 16:29 UTC] derick@php.net
-Status: Re-Opened +Status: Not a bug
 [2022-07-22 16:29 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

The combination of the "yyyyWww" and "x dayname" stanzas is not supported.

However, in order to find the Friday of a week, you can use "yyyyWww-d", such as "2019W16-5". We can't support every string that you throw at the parser :-).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC