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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 21:01:27 2025 UTC