php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26565 strtotime('this month') resolving to the wrong month?
Submitted: 2003-12-09 13:09 UTC Modified: 2011-03-30 23:38 UTC
From: michaelw at darkhorse dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 4.3.4 OS: Mac OS X 10.2.x
Private report: No CVE-ID: None
 [2003-12-09 13:09 UTC] michaelw at darkhorse dot com
Description:
------------
strtotime adds an extra month when using the phrase 
"this month" or "next month".  Using '+1 months' or '+0 
months' works fine, though.

And, no, I'm not adding a month to 'Jan. 30'.

Reproduce code:
---------------
echo date('Y-m-d', strtotime('last month'));
echo date('Y-m-d', strtotime('this month'));
echo date('Y-m-d', strtotime('next month'));

echo date('Y-m-d', strtotime('-1 months'));
echo date('Y-m-d', strtotime('+0 months'));
echo date('Y-m-d', strtotime('+1 months'));


Expected result:
----------------
2003-11-09
2003-12-09
2004-01-09

2003-11-09
2003-12-09
2004-01-09


Actual result:
--------------
2003-11-09
2004-01-09
2004-02-09

2003-11-09
2003-12-09
2004-01-09


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-09 14:37 UTC] sniper@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.


And this is expected behaviour. (this, next, etc. are FUZZY, rtfm)


 [2003-12-10 05:57 UTC] mgf@php.net
More to the point, "next" is specifically equated to "2" (or "+2") in the documentation referenced by the strtotime() manual page, so your expectation for "next month" is simply wrong.

However, "this" is also specifically equated to a "zero-valued displacement ... preferred in date strings like `this thursday'" (which doesn't sound very "fuzzy" to me), so the behaviour of "this month" in your example is, at the least, questionable.  On this basis, I'm re-opening this bug (with a revised summary) so a developer who knows their way round strtotime() can take another look.
 [2003-12-10 09:06 UTC] sniper@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 [2011-03-30 19:45 UTC] fortizma at gmail dot com
Based on the current date which is march 30, 2011.

if I try echo date('Y-m-d', strtotime('-1 months')); I would expect 2011-02-28, 
but what I'm getting is 2011-03-02.

Do this occur due to 2011 is not a leap year?
 [2011-03-30 19:48 UTC] derick@php.net
-Assigned To: +Assigned To: derick
 [2011-03-30 20:28 UTC] fortizma at gmail dot com
Derik, thanks for your reply, but in my last reply, I'm talking about last month, 
no next month. 

Thanks in advance.
 [2011-03-30 22:01 UTC] rbredlau at broadlux dot com
I am having the exact same error as fortizma.

<?php
$runtm = time();
$month = strtotime( '-1 month', $runtm );
$ts = date( 'n/j/Y g:i a', $runtm );
$ts2 = date( 'n/j/Y g:i a', $month );
$last = date( 'Y-m-F', $month );
echo $ts . PHP_EOL . $ts2 . PHP_EOL . $last . PHP_EOL . PHP_EOL;

Produces:
3/30/2011 12:56 pm
3/2/2011 12:56 pm
2011-03-March

PHP 5.2.14-0.dotdeb.0 with Suhosin-Patch 0.9.7 (cli) (built: Jul 25 2010 08:53:19)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
    with NuSphere PhpExpress v2.0.3, Copyright (c) 2002-2009 NuSphere Corp., by Dmitri Dmitrienko
    with DBG v3.9.2, (C) 2000,2010, by Dmitri Dmitrienko

cat /etc/issue
Ubuntu 9.04 \n \l
 [2011-03-30 22:39 UTC] rasmus@php.net
To the people adding comments about +-1 month here. This really isn't a bug. Read the link Derick provided:

http://derickrethans.nl/obtaining-the-next-month-in-php.html

It doesn't matter if you are going ahead 1 month or back 1 month. The logic is the same. It tries to find Feb.30 which doesn't exist, so instead of walking backwards to Feb.28 it assumes you meant 2 days after Feb.28 which is obviously in March. Like I said in the comments at that link, for better or worse, this is the UNIX convention for time manipulation. Most UNIX tools will behave exactly like this. In order to do this correctly, you should be specifying a day in the month as Derick suggests in that post.
 [2011-03-30 23:34 UTC] fortizma at gmail dot com
Ok Rasmus thanks for your reply. 

However in my opinion is not a correct behavioural. So if I ask today march, 30 
for the "last month" the function should return feb 28. The function should not 
assumes that I meant 2 days after Feb.28, when I'm just passing as param 
lastmonth. 

Anyways, in my opiniĆ³n the function is not working properly, because if I'm 
asking about lastmonth the answer should not be the current month. 

how you can create a function working properly, that return the correct last 
month every day in PHP 5.2.x.
 [2011-03-30 23:38 UTC] derick@php.net
-Block user comment: No +Block user comment: Yes
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Nov 05 19:01:29 2024 UTC