|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 06:00:01 2025 UTC |
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 PravinIt 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.