php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70186 strtotime -1 month has a bug in 2015-07-31
Submitted: 2015-08-03 03:00 UTC Modified: 2015-08-03 03:23 UTC
From: vb2005xu at qq dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.6.11 OS: all
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vb2005xu at qq dot com
New email:
PHP Version: OS:

 

 [2015-08-03 03:00 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-03 03:09 UTC] 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:23 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-08-03 03:23 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 14 00:01:34 2024 UTC