php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #66988 Modifying a time -1 month from the end of the month has unexpected results
Submitted: 2014-03-31 15:53 UTC Modified: 2014-03-31 20:25 UTC
Votes:9
Avg. Score:4.1 ± 0.9
Reproduced:9 of 9 (100.0%)
Same Version:3 (33.3%)
Same OS:2 (22.2%)
From: jglw at live dot co dot uk Assigned:
Status: Wont fix Package: Date/time related
PHP Version: 5.4.26 OS: Windows 7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jglw at live dot co dot uk
New email:
PHP Version: OS:

 

 [2014-03-31 15:53 UTC] jglw at live dot co dot uk
Description:
------------
If you modify a time -1 month from the last day of a month it seems to interpret that as take away the number of days from the previous month. It would make more sense that if you modified a time by -1 month it wouldn't fall into the same month from which you modified it.

Test script:
---------------
<?php
$time = strtotime("2014-03-31 -1 month");
echo date("Y-m-d", $time);
?>

Expected result:
----------------
2014-02-28

Actual result:
--------------
2014-03-03

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-03-31 16:26 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-03-31 16:26 UTC] requinix@php.net
"-1 month" works by literally subtracting 1 from the month. The resulting date 2014-02-31 overflows to 2014-03-03.
There is no solution to the -1 problem that satisfies everyone. If you need it to become 2014-02-28 then apply a bit more logic: if the two months are the same then use the last day of the previous month instead.
 [2014-03-31 20:13 UTC] jglw at live dot co dot uk
-Type: Bug +Type: Feature/Change Request
 [2014-03-31 20:13 UTC] jglw at live dot co dot uk
Might I suggest that you could set in the php.ini something like date.overflow to true or false, defaulting to true. If false it would give the last day of the month in these cases.
 [2014-03-31 20:25 UTC] derick@php.net
No settings for this, that makes for unportable scripts.

Often, when you do -1 month, you are really not interested in the date itself, but just in the month. In which case you can use "first day of last month" or "first day of -2 months". And if you want the last day of the previous month, then you can use "last day of last month".
 [2014-03-31 20:25 UTC] derick@php.net
-Status: Not a bug +Status: Wont fix
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Jan 27 05:03:33 2022 UTC