Req #78766 [Opn->Nab]: date('Y-m-d', '-1 month') don't work as expected n day 31
| From: | requinix@php.net | Date: | Thu, 31 Oct 2019 14:48:20 +0000 |
| Subject: | Req #78766 [Opn->Nab]: date('Y-m-d', '-1 month') don't work as expected n day 31 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-223538@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78766&edit=1
ID: 78766
Updated by: requinix@php.net
Reported by: gabrielklasso at gmail dot com
Summary: date('Y-m-d', '-1 month') don't work as expected n
day 31
-Status: Open
+Status: Not a bug
Type: Feature/Change Request
Package: Date/time related
Operating System: Any
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
It isn't rounding. 2019-10-31 - 1 month = 2019-09-31 (literally 10-1=9) which overflows to
2019-10-01.
If you have a specific desired behavior in mind to deal with different month lengths then you need
to write specific code to make it happen that way.
Previous Comments:
------------------------------------------------------------------------
[2019-10-31 14:39:56] gabrielklasso at gmail dot com
Description:
------------
When I run date('m', '-1 month'), I expect to get the number of the previous
month, but it is not what always happen.
Today is 2019-10-31, and when I run it I got '10'.
To understand better what is happening, I tried to run date('Y-m-d', '-1 month')
and I got '2019-10-01'.
I believe it is because there is no day 2019-09-31, so it rounds up. But IMO it should round down in
this case.
Test script:
---------------
$lastMonth = date('m', '-1 month');
echo $lastMonth;
Expected result:
----------------
'9'
Actual result:
--------------
'10'
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78766&edit=1