php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78766 date('Y-m-d', '-1 month') don't work as expected n day 31
Submitted: 2019-10-31 14:39 UTC Modified: 2019-10-31 14:48 UTC
From: gabrielklasso at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gabrielklasso at gmail dot com
New email:
PHP Version: OS:

 

 [2019-10-31 14:39 UTC] 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'

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-10-31 14:48 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-10-31 14:48 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 07:01:27 2024 UTC