Bug #68981 [Com]: negative relative month values in strtime() give nonsense results

From: Date: Tue, 03 Feb 2015 20:37:20 +0000
Subject: Bug #68981 [Com]: negative relative month values in strtime() give nonsense results
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-190454@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68981&edit=1 ID: 68981 Comment by: teo8976 at gmail dot com Reported by: teo8976 at gmail dot com Summary: negative relative month values in strtime() give nonsense results Status: Open Type: Bug Package: Date/time related Operating System: linux PHP Version: 5.6.5 Block user comment: N Private report: N New Comment: Indeed I think you are right that the current behavior makes sense, but documentation then is definitely wrong and needs to be corrected. See https://bugs.php.net/bug.php?id=60353#1422995592 Previous Comments: ------------------------------------------------------------------------ [2015-02-03 20:18:52] teo8976 at gmail dot com Well, then the documentation doesn't accurately describe the behavior. From http://php.net/manual/en/datetime.formats.relative.php "Note: Relative month values are calculated BASE ON THE LENGTH OF MONTHS THEY PASS THROUGH. An example would be "+2 month 2011-11-30", which would produce "2012-01-30". This is due to November being 30 days in length, and December being 31 days in length, producing a total of 61 days." The only sensible way to extend that reasoning (the one in documentation, which is completely different from yours) to subtractions is to assume that the number of days subtracted is the number of days of the month to which the starting date belong, plus the number of days of the previous month, etc. According to that statement in documentation, relative formats with dates are not supposed to add/subtract to the index of the month and then "wrap". They are supposed to add/subtract the number of days that correspond to each month. Instead, it does seem to behave the way you describe, even for positive months. So either the documentation or the behavior needs to be fixed. It would definitely make more sense to fix the behavior, because the one described in the documentation is much more desirable and intuitive. ------------------------------------------------------------------------ [2015-02-03 19:04:35] requinix@php.net You told strtotime() to subtract months from a date so it did that. But rather than error out when it arrived at dates like November 31st or February 31st, it wrapped the date to the next month. Like mktime() would have. If you want the last day of each month then you have to say that. http://3v4l.org/QSorH ------------------------------------------------------------------------ [2015-02-03 18:36:01] teo8976 at gmail dot com Description: ------------ The way negative relative month values are handled by strtotime() is 1) undocumented 2) complete nonsense example: strtotime("-3 months", $date) Test script: --------------- $end_date=mktime(23,59,59,12,31,2014); echo date("r", $end_date)."\n"; $date=strtotime("-1 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-2 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-3 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-4 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-5 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-6 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-7 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-8 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-9 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-10 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-11 months", $end_date); echo date("r", $date)."\n"; $date=strtotime("-12 months", $end_date); echo date("r", $date)."\n"; Expected result: ---------------- Wed, 31 Dec 2014 23:59:59 +0100 Mon, 30 Nov 2014 23:59:59 +0100 Fri, 31 Oct 2014 23:59:59 +0100 Wed, 30 Sep 2014 23:59:59 +0200 Sun, 31 Aug 2014 23:59:59 +0200 Thu, 31 Jul 2014 23:59:59 +0200 Tue, 30 Jun 2014 23:59:59 +0200 Sat, 31 May 2014 23:59:59 +0200 Thu, 30 Apr 2014 23:59:59 +0200 Mon, 31 Mar 2014 23:59:59 +0200 Mon, 28 Feb 2014 23:59:59 +0100 Fri, 31 Jan 2014 23:59:59 +0100 Tue, 31 Dec 2013 23:59:59 +0100 Actual result: -------------- Wed, 31 Dec 2014 23:59:59 +0100 Mon, 01 Dec 2014 23:59:59 +0100 Fri, 31 Oct 2014 23:59:59 +0100 Wed, 01 Oct 2014 23:59:59 +0200 Sun, 31 Aug 2014 23:59:59 +0200 Thu, 31 Jul 2014 23:59:59 +0200 Tue, 01 Jul 2014 23:59:59 +0200 Sat, 31 May 2014 23:59:59 +0200 Thu, 01 May 2014 23:59:59 +0200 Mon, 31 Mar 2014 23:59:59 +0200 Mon, 03 Mar 2014 23:59:59 +0100 << THIS IS PARTICULARLY HILARIOUS Fri, 31 Jan 2014 23:59:59 +0100 Tue, 31 Dec 2013 23:59:59 +0100 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68981&edit=1

« previous php.bugs (#190454) next »