php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78355 Getting the last day of a given month failing at months of 31 days
Submitted: 2019-07-31 02:11 UTC Modified: 2019-07-31 02:21 UTC
From: belbayar at mobicom dot mn Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.3Git-2019-07-31 (Git) OS: Centos 7
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: belbayar at mobicom dot mn
New email:
PHP Version: OS:

 

 [2019-07-31 02:11 UTC] belbayar at mobicom dot mn
Description:
------------
I have a problem like this. For a given date I need to get the last day of the previous month. 

For example 
today is 2019-07-31, I needed date is 2019-06-30 



Test script:
---------------
echo date('Y-m-t', strtotime("2019-07-31" . '-1 months'));
echo date("Y-m-d", strtotime("-1 Month"));

Expected result:
----------------
2019-07-31
2019-07-01

Actual result:
--------------
2019-06-30
2019-06-01 or 2019-06-30 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-07-31 02:21 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-07-31 02:21 UTC] requinix@php.net
Your Expected and Actual are backwards.

2019-07-31 - 1 month = 2019-06-31 which overflows to 2019-07-01.

strtotime() supports a lot of expressions. One of them fits your needs exactly.
https://www.php.net/manual/en/datetime.formats.relative.php
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Apr 13 04:01:23 2021 UTC