Bug #51446 [Com]: strtotime(last month) should always give the previous month in results

From: Date: Fri, 22 Mar 2019 07:55:44 +0000
Subject: Bug #51446 [Com]: strtotime(last month) should always give the previous month in results
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220123@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=51446&edit=1

 ID:                 51446
 Comment by:         jmcustiel at gmail dot com
 Reported by:        marques at displague dot com
 Summary:            strtotime(last month) should always give the
                     previous month in results
 Status:             Not a bug
 Type:               Bug
 Package:            Date/time related
 PHP Version:        5.3.2
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2011-05-04 14:01:23] 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

------------------------------------------------------------------------
[2010-03-31 17:45:13] 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.

.

------------------------------------------------------------------------
[2010-03-31 17:10:20] 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
)



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=51446&edit=1


Thread (5 messages)

« previous php.bugs (#220123) next »