#50682 [Bgs]: strtotime producing incorrect result
| From: | keefeg at gmail dot com | Date: | Thu, 07 Jan 2010 01:37:43 +0000 |
| Subject: | #50682 [Bgs]: strtotime producing incorrect result | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-146400@lists.php.net to get a copy of this message | ||
ID: 50682
User updated by: keefeg at gmail dot com
Reported By: keefeg at gmail dot com
Status: Bogus
Bug Type: Date/time related
Operating System: FreeBSD 6.2
PHP Version: 5.2.12
New Comment:
I understand your explanation, but the results still seem incorrect.
Under reasonable circumstances even programmers shouldn't have to
consider "last month" to be equal to "this month."
In the context of "last month", it should first decrement the month,
and then if that particular day (31st of November) does not exist, it
should then decrement the day until it reaches a valid day (30th of
November).
The way it is now breaks programs in a non-obvious way. I use that
code to daily make a list of archive files to "keep" (now, -1 month, -2
month, -3 month) and on the 31st of December it ended up deleting the
november file because it thought "-1 month" was December.
I'll work around using the 1st of the month, but it at least merits a
warning in the documentation.
Thanks for PHP, it's incredibly useful!
Previous Comments:
------------------------------------------------------------------------
[2010-01-06 20:53:58] rasmus@php.net
That's because you are asking for the same day last month which doesn't
exist since November doesn't have 31 days. November 31 gets normalized
to Dec 1. If you are using last/next make sure you feed it a recurring
starting point. Like the 1st of the sequence, in this case the 1st of
the month.
------------------------------------------------------------------------
[2010-01-06 20:28:57] keefeg at gmail dot com
Description:
------------
strtotime does not handle the "last month" parameter correctly all the
time.
Reproduce code:
---------------
---
From manual page: function.strtotime#Description
---
$ny = strtotime("31 December 2009");
echo "this month: " . date('Ym', strtotime("now", $ny)) .
"\n";
echo "last month: " . date('Ym', strtotime("last month", $ny)) .
"\n";
Expected result:
----------------
I would like it to produce this result:
this month: 200912
last month: 200911
Actual result:
--------------
It prints this result:
this month: 200912
last month: 200912
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50682&edit=1