Bug #70186 [Opn->Nab]: strtotime -1 month has a bug in 2015-07-31
Edit report at https://bugs.php.net/bug.php?id=70186&edit=1
ID: 70186
Updated by: requinix@php.net
Reported by: vb2005xu at qq dot com
Summary: strtotime -1 month has a bug in 2015-07-31
-Status: Open
+Status: Not a bug
Type: Bug
Package: Date/time related
Operating System: all
PHP Version: 5.6.11
Block user comment: N
Private report: N
New Comment:
strtotime() handles addition and subtraction the same way functions like mktime() do: do the math
according to how you told it, then fix the date so it's valid. "-1 month" will
subtract one from the month resulting in 2015-06-31, which overflows to 2015-07-01.
If you want the last day of the previous month then you must to explicitly tell it to do that (eg, I
believe "last day of previous month" will do that). Otherwise you need to figure out how
you want your application to behave with this edge case.
Previous Comments:
------------------------------------------------------------------------
[2015-08-03 03:09:45] vb2005xu at qq dot com
Is not just in July 31st, March 31st, May 31st, July 31st, October 31st, December 31st, there is
this problem, as long as the number of days in January is less than the current number of days will
appear this problem
------------------------------------------------------------------------
[2015-08-03 03:00:52] vb2005xu at qq dot com
Description:
------------
<?php
date_default_timezone_set('Asia/Shanghai');
$tn = strtotime("2015-07-31");
echo date('Y-m' , strtotime("-1 month",$tn));
// why output 2015-07 ?
Test script:
---------------
<?php
date_default_timezone_set('Asia/Shanghai');
$tn = strtotime("2015-07-31");
echo date('Y-m' , strtotime("-1 month",$tn));
Expected result:
----------------
2015-06
Actual result:
--------------
2015-07
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70186&edit=1
Thread (3 messages)