php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53212 Date not rolling back one month
Submitted: 2010-10-31 04:21 UTC Modified: 2010-10-31 04:32 UTC
From: karl at crucialp dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS: Centos5 64bit
Private report: No CVE-ID: None
 [2010-10-31 04:21 UTC] karl at crucialp dot com
Description:
------------
Howdy,

This is a php implementation on a cpanel box:
PHP 5.2.9 (cli) (built: Jun  5 2009 04:19:37) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
    with the ionCube PHP Loader v3.3.20, Copyright (c) 2002-2010, by ionCube Ltd., and
    with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies
    with Suhosin v0.9.27, Copyright (c) 2007, by SektionEins GmbH
    with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies


Today is 2010 - 10 - % and basically I have the following:

$thisMonth = date("Y-m-1");
this will return as expected: 2010-10-1

Then I have this:
$lastmonth = strftime("%Y-%m-1", strtotime("-1 months"));
Which does not return as expected, it returns: 2010-10-1 the same as above.
Expected result would be: 2010-09-1
However I have tried alternative approaches such as naming.
$lastmonth = strftime("%Y-%m-1", strtotime("-1 month"));
Same thing.

Possible bug?

Test script:
---------------
<?php


echo date("Y-m-1"); //Returns 2010-10-1

echo "<br />";

echo strftime("%Y-%m-1", strtotime("-2 month")); // Returns 2010-08-1

echo "<br />";

echo strftime("%Y-%m-1", strtotime("-1 month")); // Unexpected: 2010-10-1

echo "<br />"; 

echo strftime("%Y-%m-1", strtotime("-1 months")); // Unexpected: 2010-10-1

?>

Expected result:
----------------
I expect that it would return a back roll of on month from the current

At the moment of posting this it is october, so it should roll to september, delivering results:

2010-09-1



Actual result:
--------------
Well at the moment it is just returning the current month:

2010-10-1



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-31 04:32 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-10-31 04:32 UTC] rasmus@php.net
Since it is 10-31 where you are, -1 month tries to go back to the 31st day of 
September, but since there is no 31st day of September, that becomes Oct.1 
instead.  That's just how strtotime works.  You need to be more precise when you 
say -1 month.  For example, you could do strtotime('last day of last month') or 
'first day of 3 months ago', things like that.  But just saying 'last month' is 
not descriptive enough.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 15:01:33 2024 UTC