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:10
Avg. Score:4.1 ± 0.8
Reproduced:10 of 10 (100.0%)
Same Version:3 (30.0%)
Same OS:2 (20.0%)
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC