php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78827 Wrong timestamp
Submitted: 2019-11-17 21:22 UTC Modified: 2020-01-20 17:11 UTC
From: obreham at gmail dot com Assigned: derick (profile)
Status: Assigned Package: Date/time related
PHP Version: 7.3.11 OS: Ubuntu 16.04.6 LTS
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
20 - 15 = ?
Subscribe to this entry?

 
 [2019-11-17 21:22 UTC] obreham at gmail dot com
Description:
------------
---
From manual page: https://php.net/function.strftime
---

When adding a time interval to 'last Sunday' on a Sunday, you get an incorrect timestamp when interval is longer than a week.  'last Sunday' becomes 'this Sunday'.

Test script:
---------------
/* Example code was run on Sunday Nov 17, 2019 */

var_dump(
$saturday = strtotime('last Saturday'),
(strtotime('last Saturday + 1 day') - $saturday) / 86400,
(strtotime('last Saturday + 2 day') - $saturday) / 86400,
(strtotime('last Saturday + 3 day') - $saturday) / 86400,
(strtotime('last Saturday + 4 day') - $saturday) / 86400,
(strtotime('last Saturday + 5 day') - $saturday) / 86400,
(strtotime('last Saturday + 6 day') - $saturday) / 86400,
(strtotime('last Saturday + 7 day') - $saturday) / 86400,
(strtotime('last Saturday + 8 day') - $saturday) / 86400,
(strtotime('last Saturday + 9 day') - $saturday) / 86400,
$sunday = strtotime('last Sunday'),
(strtotime('last Sunday + 1 day') - $sunday) / 86400,
(strtotime('last Sunday + 2 day') - $sunday) / 86400,
(strtotime('last Sunday + 3 day') - $sunday) / 86400,
(strtotime('last Sunday + 4 day') - $sunday) / 86400,
(strtotime('last Sunday + 5 day') - $sunday) / 86400,
(strtotime('last Sunday + 6 day') - $sunday) / 86400,
(strtotime('last Sunday + 7 day') - $sunday) / 86400,
(strtotime('last Sunday + 8 day') - $sunday) / 86400,
(strtotime('last Sunday + 9 day') - $sunday) / 86400,
$monday = strtotime('last Monday'),
(strtotime('last Monday + 1 day') - $monday) / 86400,
(strtotime('last Monday + 2 day') - $monday) / 86400,
(strtotime('last Monday + 3 day') - $monday) / 86400,
(strtotime('last Monday + 4 day') - $monday) / 86400,
(strtotime('last Monday + 5 day') - $monday) / 86400,
(strtotime('last Monday + 6 day') - $monday) / 86400,
(strtotime('last Monday + 7 day') - $monday) / 86400,
(strtotime('last Monday + 8 day') - $monday) / 86400,
(strtotime('last Monday + 9 day') - $monday) / 86400
);

Expected result:
----------------
int(1573880400)
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(7)
int(8)
int(9)
int(1573362000)
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(7)
int(8)
int(9)
int(1573448400)
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(7)
int(8)
int(9)

Actual result:
--------------
int(1573880400)
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(7)
int(8)
int(9)
int(1573362000)
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(14)
int(15)
int(16)
int(1573448400)
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(7)
int(8)
int(9)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-11-18 08:11 UTC] cmb@php.net
-Package: Calendar related +Package: Date/time related
 [2019-11-23 09:08 UTC] cmb@php.net
Isn't that suppossed to be `Sunday last week` instead of `last
Sunday`?
 [2019-11-23 13:56 UTC] obreham at gmail dot com
No matter what 'last Sunday' means, when you add '7 day' to it, it should yield 1 more day when compared to adding '6 day' to it; not 8 days.

I've been also looking at the documentation (https://www.php.net/manual/en/datetime.formats.relative.php), which gives the following caution:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~
«Also observe that the "of" in "ordinal space dayname space 'of' " and "'last' space dayname space 'of' " does something special.

[...]

"'last' dayname" takes the last dayname from the current day. (Example: "last wed july 2008" means "2008-06-25"; "july 2008" first sets the current date to "2008-07-01" and then "last wed" moves to the previous Wednesday which is "2008-06-25").»
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

But that doesn't explain why 'last Sunday' means something different in 'last Sunday + 6 day' and 'last Sunday + 7 day'.

Note that 'last Sunday + 6 day', calculated on a Sunday, should return the equivalent of 'yesterday' or 'last Saturday' (and it does). And 'last Sunday + 7 day', calculated on a Sunday, should return the equivalent of 'today'. It actually returns 'next Sunday'.
 [2019-11-29 18:53 UTC] leo dot sjoberg at gmail dot com
I can confirm that this is a more general problem that occurs when using strtotime("last $x + $n") where $x is the current weekday, and $n >= 7.

e.g running it with "last saturday + 7 days" on a saturday will yield the same results.

Note that it only happens for positive numbers greater than or equal to 7, and does not occur for negative numbers.

I'm looking through the source now to see if I can find anything relating to this.
 [2019-11-30 08:42 UTC] leo dot sjoberg at gmail dot com
I was just informed that this is a duplicate of an already existing bug: #78080.
 [2019-12-01 14:39 UTC] leo dot sjoberg at gmail dot com
PR open at timelib: https://github.com/derickr/timelib/pull/71
 [2020-01-20 17:11 UTC] girgias@php.net
-Assigned To: +Assigned To: derick
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Aug 06 13:01:26 2020 UTC