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
 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 08 14:01:28 2024 UTC