php.net |  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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
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
Description:
------------
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"

or

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"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-30 18:51 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-07-30 18:51 UTC] requinix@php.net
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: Fri May 03 17:01:32 2024 UTC