|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79916 Inconsistency when modifying date
Submitted: 2020-07-30 11:47 UTC Modified: 2020-07-30 18:51 UTC
From: david dot kmenta at me dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS:
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.
Block user comment
Status: Assign to:
Bug Type:
From: david dot kmenta at me dot com
New email:
PHP Version: OS:


 [2020-07-30 11:47 UTC] david dot kmenta at me dot com
When I'm adding (for example) months to the datetime, it shouldn't matter if I add months one-by-one by calling the "modify" method multiple times, or at once with one call.

I know it's caused by the February and I don't care if "1 month" means 30, 31, ... days, but I think that there should be a consistent result regardless the way the "modify" method is used.

Test script:
$today = new \DateTimeImmutable('2020-01-30 11:00:00');

var_dump($today->modify('+2 months')->format('Y-m-d H:i:s'));
var_dump($today->modify('+1 month')->modify('+1 month')->format('Y-m-d H:i:s'));

Expected result:
string(19) "2020-03-30 11:00:00"
string(19) "2020-03-30 11:00:00"


string(19) "2020-04-01 11:00:00"
string(19) "2020-04-01 11:00:00"

Actual result:
string(19) "2020-03-30 11:00:00"
string(19) "2020-04-01 11:00:00"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-30 18:51 UTC]
-Status: Open +Status: Not a bug
 [2020-07-30 18:51 UTC]
DateTime doesn't "remember" the original date. If you say +1 month then the date moves forward one month. And if the date overflows because you tried to get day 31 of a month with only 30 (yes, including February) then the date overflows. If you say +1 month again then the date moves forward one month *from that point*.

January 30 + 1 month + 1 month
= March 1/2 (because "February 30" overflowed) + 1 month
= April 1/2

PHP and most of the GNU world works this way. Others don't. Dates are hard.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jul 21 06:01:30 2024 UTC