Bug #79916 [Opn->Nab]: Inconsistency when modifying date

From: Date: Thu, 30 Jul 2020 18:51:23 +0000
Subject: Bug #79916 [Opn->Nab]: Inconsistency when modifying date
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228347@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79916&edit=1

 ID:                 79916
 Updated by:         requinix@php.net
 Reported by:        david dot kmenta at me dot com
 Summary:            Inconsistency when modifying date
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Date/time related
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2020-07-30 11:47:03] 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"


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=79916&edit=1


Thread (2 messages)

« previous php.bugs (#228347) next »