php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51446 strtotime(last month) should always give the previous month in results
Submitted: 2010-03-31 17:10 UTC Modified: 2019-03-22 15:54 UTC
From: marques at displague dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.3.2 OS:
Private report: No CVE-ID: None
 [2010-03-31 17:10 UTC] marques at displague dot com
Description:
------------
strtotime("last month") should always return a date in the previous month.

Instead, the algorithm seems to return the current day minus the number of days 
in 
the previous month.

If the number of days in the previous month is less than the current day of the 
month then the current day of the month days should be subtracted. 

I realize that Bug #50682, Bug #27793, Bug #27786, Bug #22486 are related, but I 
do not believe that PHP is strictly implementing the strtotime() as it exists 
elsewhere and is in a position to provide a better experience.  Just as "last 
day" and "first day" were added in PHP 5.3 there is room for improvement.

Implementations are not ubiquitous as MySQL, for example, provides "2010-02-28" 
when given 'select "2010-03-31" - interval 1 month;'.  This is the sort of 
behavior I would expect from PHP's strtotime().



Test script:
---------------
date_default_timezone_set('America/New_York');
print_r(array(
date(DATE_RFC822,strtotime("last month", strtotime("january 31 2010"))),
date(DATE_RFC822,strtotime("last month", strtotime("february 28 2010"))),
date(DATE_RFC822,strtotime('march 28 2010 -1 month')),
date(DATE_RFC822,strtotime('march 29 2010 -1 month')),
date(DATE_RFC822,strtotime('march 30 2010 -1 month')),
date(DATE_RFC822,strtotime('march 31 2010 -1 month')),
date(DATE_RFC822,strtotime("last month", strtotime("april 30 2010"))),
date(DATE_RFC822,strtotime("last month", strtotime("may 31 2010"))),
date(DATE_RFC822,strtotime("last month", strtotime("june 30 2010"))),
date(DATE_RFC822,strtotime("last month", strtotime("july 31 2010"))),
date(DATE_RFC822,strtotime("last month", strtotime("august 31 2010"))),
date(DATE_RFC822,strtotime("last month", strtotime("september 30 2010"))),
date(DATE_RFC822,strtotime("last month", strtotime("october 31 2010"))),
date(DATE_RFC822,strtotime("last month", strtotime("november 30 2010"))),
date(DATE_RFC822,strtotime("last month", strtotime("december 31 2010"))),
date(DATE_RFC822,strtotime('last month', strtotime("march 29 2012"))),
));


Expected result:
----------------
Array
(
    [0] => Thu, 31 Dec 09 00:00:00 -0500
    [1] => Sun, 31 Jan 10 00:00:00 -0500
    [2] => Sun, 28 Feb 10 00:00:00 -0500
    [3] => Sun, 28 Feb 10 00:00:00 -0500
    [4] => Sun, 28 Feb 10 00:00:00 -0500
    [5] => Sun, 28 Feb 10 00:00:00 -0500
    [6] => Wed, 31 Mar 10 00:00:00 -0500
    [7] => Fri, 30 Apr 10 00:00:00 -0400
    [8] => Mon, 31 May 10 00:00:00 -0400
    [9] => Wed, 30 Jun 10 00:00:00 -0400
    [10] => Sat, 31 Jul 10 00:00:00 -0400
    [11] => Tue, 31 Aug 10 00:00:00 -0400
    [12] => Thu, 30 Sep 10 00:00:00 -0400
    [13] => Sun, 31 Oct 10 00:00:00 -0400
    [14] => Tue, 30 Nov 10 00:00:00 -0500
    [15] => Wed, 29 Feb 12 00:00:00 -0500
)


Actual result:
--------------
Array
(
    [0] => Thu, 31 Dec 09 00:00:00 -0500
    [1] => Thu, 28 Jan 10 00:00:00 -0500
    [2] => Sun, 28 Feb 10 00:00:00 -0500
    [3] => Mon, 01 Mar 10 00:00:00 -0500
    [4] => Tue, 02 Mar 10 00:00:00 -0500
    [5] => Wed, 03 Mar 10 00:00:00 -0500
    [6] => Tue, 30 Mar 10 00:00:00 -0400
    [7] => Sat, 01 May 10 00:00:00 -0400
    [8] => Sun, 30 May 10 00:00:00 -0400
    [9] => Thu, 01 Jul 10 00:00:00 -0400
    [10] => Sat, 31 Jul 10 00:00:00 -0400
    [11] => Mon, 30 Aug 10 00:00:00 -0400
    [12] => Fri, 01 Oct 10 00:00:00 -0400
    [13] => Sat, 30 Oct 10 00:00:00 -0400
    [14] => Wed, 01 Dec 10 00:00:00 -0500
    [15] => Wed, 29 Feb 12 00:00:00 -0500
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-31 17:45 UTC] derick@php.net
-Status: Open +Status: Bogus
 [2010-03-31 17:45 UTC] derick@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

.
 [2011-05-04 14:01 UTC] scspl dot pravin at gmail dot com
Hi,

I am converting '12-31-2011' To 2011-12-31 using Function echo date('Y-m-d',strtotime('12-31-2011')); But it is not working.

Thanks
Pravin
 [2019-03-22 07:55 UTC] jmcustiel at gmail dot com
It seems this is still happening (PHP 7.2). 
<?php 
echo (new DateTime('2019-03-31 -1 month'))->format('Y-m-d H:i:s'); // 2019-03-03 00:00:00

// But I would expect this result
echo (new DateTime('2019-03-31 -31 days'))->format('Y-m-d H:i:s'); // 2019-02-28 00:00:00

And it does not behaves according to the documentation:
In the documentation there's this note:

Relative month values are calculated based on the length of months that they pass through. An example would be "+2 month 2011-11-30", which would produce "2012-01-30". This is due to November being 30 days in length, and December being 31 days in length, producing a total of 61 days. 

When at the end of the month, the most days it passes through are from March.
 [2019-03-22 15:54 UTC] requinix@php.net
> It seems this is still happening (PHP 7.2).
Yes, but it still isn't a bug. The simplest explanation for how "+N unit" works is that it adds +N to whatever the unit is. -1 month subtracts 1 from the month number. When the result is an invalid date it over-/underflows to correct.
This is also how most of the GNU/Linux community handles troublesome date math.

2019-03-31 -1 month = 2019-02-31 which overflows to 2019-03-03

If your application wants to end up with the last day of the previous month then it needs to codify that.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jul 20 07:01:28 2024 UTC