Bug #68981 [Opn->Nab]: negative relative month values in strtime() give nonsense results

From: Date: Tue, 03 Feb 2015 19:04:35 +0000
Subject: Bug #68981 [Opn->Nab]: negative relative month values in strtime() give nonsense results
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-190446@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
 Updated by:         requinix@php.net
 Reported by:        teo8976 at gmail dot com
 Summary:            negative relative month values in strtime() give
                     nonsense results
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Date/time related
 Operating System:   linux
 PHP Version:        5.6.5
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[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


Thread (7 messages)

« previous php.bugs (#190446) next »