Doc #60353 [Com]: Date-related "Relative Formats" doesn't indicate month behaviour
| From: | teo8976 at gmail dot com | Date: | Tue, 03 Feb 2015 18:28:35 +0000 |
| Subject: | Doc #60353 [Com]: Date-related "Relative Formats" doesn't indicate month behaviour | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-11918@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60353&edit=1
ID: 60353
Comment by: teo8976 at gmail dot com
Reported by: frozenfire@php.net
Summary: Date-related "Relative Formats" doesn't indicate
month behaviour
Status: Closed
Type: Documentation Problem
Package: Documentation problem
PHP Version: Irrelevant
Assigned To: frozenfire
Block user comment: N
Private report: N
New Comment:
@frozenfire Hey man you forgot NEGATIVE values.
The current explanation clearly doesn't explain how negative month values are handled.
31 december -1 months -> 1 december
31 december -2 months -> 31 october
31 december -3 months -> 1 october
31 december -4 months -> 31 august
Documentation definitely needs to explain what the hell the logic behind that is, because it
doesn't make any sense.
Previous Comments:
------------------------------------------------------------------------
[2011-11-27 03:59:00] frozenfire@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation better.
------------------------------------------------------------------------
[2011-11-27 03:58:45] frozenfire@php.net
Automatic comment from SVN on behalf of frozenfire
Revision: http://svn.php.net/viewvc/?view=revision&revision=320010
Log: Added note regarding the behaviour of relative months. Closes bug #60353.
------------------------------------------------------------------------
[2011-11-21 22:05:44] frozenfire@php.net
Description:
------------
A fellow on reddit.com/r/PHP mentioned that the documentation for Relative
Formats for dates is missing information on how relative months work.
Here's the relevant comment:
http://www.reddit.com/r/PHP/comments/mk3ub/strtotime_wtf/c31lhav
"echo date("Y-m-d", strtotime("+1 month 2011-11-30"));
returns: 2011-12-30 // wtf? but I wanted 2011-12-31
This is why you don't use months when calculating dates. A month is ambiguous.
Think about it - since months vary, which month is it supposed to use to
calculate 1 month? November or December?
Since The starting date is in November, it assumes that is the first month it
should start calculating from. So, for the first iteration, 1 month is 30 days.
However, it recalculates what a month is for every iteration.
consider this:
echo date("Y-m-d", strtotime("+1 month 2011-11-30"));
// 2011-12-30 or 30 days
echo date("Y-m-d", strtotime("+2 month 2011-11-30"));
// 2012-01-30 or 61 days
echo date("Y-m-d", strtotime("+30 days 2011-11-30"));
// 2011-12-30
echo date("Y-m-d", strtotime("+60 days 2011-11-30"));
// 2012-01-29
Notice how when I use +2 months it suddenly becomes 61 days. This is because it
starts in December on the second iteration, and it recalculates what a month is
based on that."
This behaviour should be documented in the datetime.formats.relative document.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60353&edit=1