From:             
Operating system: 
PHP version:      5.3.2
Package:          Date/time related
Bug Type:         Bug
Bug description:strtotime(last month) should always give the previous month in 
results

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 bug report at http://bugs.php.net/bug.php?id=51446&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=51446&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=51446&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=51446&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=51446&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=51446&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=51446&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=51446&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=51446&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=51446&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=51446&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=51446&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=51446&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=51446&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=51446&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=51446&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=51446&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=51446&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=51446&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=51446&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=51446&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=51446&r=mysqlcfg

Reply via email to