Bug #80610 [Opn]: DateTime calculate wrong with DateInterval
| From: | requinix@php.net | Date: | Sun, 10 Jan 2021 01:28:14 +0000 |
| Subject: | Bug #80610 [Opn]: DateTime calculate wrong with DateInterval | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-231473@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80610&edit=1
ID: 80610
Updated by: requinix@php.net
Reported by: info at mobger dot de
Summary: DateTime calculate wrong with DateInterval
Status: Open
Type: Bug
-Package: *Calendar problems
+Package: Date/time related
Operating System: Docker / PHPSandbox?
PHP Version: 7.4.14
Block user comment: N
Private report: N
New Comment:
Pretty sure there is a bug here regarding gaining/losing an hour across DST transitions, but
I'm also pretty sure it's a duplicate of one already reported somewhere in this system.
Note:
- Summer DST for Europe/Berlin began 2020-03-29 02:00:00 (DST=0) -> 03:00:00 (DST=1)
- 2020-04-12 12:00:00 (DST=1) - 20760m (14d10h) = 2020-03-29 02:00:00 (DST=1) -> 01:00:00 (DST=0)
- 2020-04-12 12:00:00 (DST=1) - 20700m (14d9h) = 2020-03-29 03:00:00 (DST=1)
For the first pairs of outputs using 20761/20760m,
// recalc 2020-03-29 00:59:00 - correct
// easter 2020-04-12 11:00:00 - incorrect, should be 12:00
// recalc 2020-03-29 03:00:00 - incorrect, should be 01:00
// easter 2020-04-12 13:00:00 - correct according to the recalc value
For the second pair using 20701/20700m,
// recalc 2020-03-29 03:59:00 - incorrect, should be 01:59:00 (no DST)
// easter 2020-04-12 13:00:00 - correct according to the recalc value
// recalc 2020-03-29 03:00:00 - correct
// easter 2020-04-12 12:00:00 - correct
Previous Comments:
------------------------------------------------------------------------
[2021-01-09 23:23:21] info at mobger dot de
It seems not to be a bug. I ignored the problem of summertime, which is not really good described in
the documentation.
$expectEaster = date_create_from_format('Y-m-d H:i:s', '2020-04-12 12:00:00',
new DateTimeZone('Europe/Berlin'));
$interval = new DateInterval('PT20761M');
$expectEaster->sub($interval);
echo('recalc '.$expectEaster->format('Y-m-d H:i:s')."\n");
$expectEaster->add($interval);
echo('easter '.$expectEaster->format('Y-m-d H:i:s')."\n" );
$expectEaster = date_create_from_format('Y-m-d H:i:s', '2020-04-12 12:00:00',
new DateTimeZone('Europe/Berlin'));
$interval = new DateInterval('PT20760M');
$expectEaster->sub($interval);
echo('recalc '.$expectEaster->format('Y-m-d H:i:s')."\n");
$expectEaster->add($interval);
echo('easter '.$expectEaster->format('Y-m-d H:i:s')."\n");
$expectEaster = date_create_from_format('Y-m-d H:i:s', '2020-04-12 12:00:00',
new DateTimeZone('Europe/Berlin'));
$interval = new DateInterval('PT20701M');
$expectEaster->sub($interval);
echo('recalc '.$expectEaster->format('Y-m-d H:i:s')."\n");
$expectEaster->add($interval);
echo('easter '.$expectEaster->format('Y-m-d H:i:s')."\n");
$expectEaster = date_create_from_format('Y-m-d H:i:s', '2020-04-12 12:00:00',
new DateTimeZone('Europe/Berlin'));
$interval = new DateInterval('PT20700M');
$expectEaster->sub($interval);
echo('recalc '.$expectEaster->format('Y-m-d H:i:s')."\n");
$expectEaster->add($interval);
echo('easter '.$expectEaster->format('Y-m-d H:i:s')."\n");
// recalc 2020-03-29 00:59:00 // remove the missing hour
// easter 2020-04-12 11:00:00 // remove the missing hour a second time via adding
// recalc 2020-03-29 03:00:00
// easter 2020-04-12 13:00:00
// recalc 2020-03-29 03:59:00
// easter 2020-04-12 13:00:00
// recalc 2020-03-29 03:00:00
// easter 2020-04-12 12:00:00
------------------------------------------------------------------------
[2021-01-09 21:42:45] info at mobger dot de
Description:
------------
I want to add and sub some minutes fram a date. I use the dateInterval for the calculation.
It seems to work with lower numbers of minutes. I have not tested it systemattically.
If I üpersonally use PHP 7.4 in my local development (ddev/docker). But I Can repoduce the bug
online https://sandbox.onlinephpfunctions.com/
with various php-versions. 8.0.0, 7.4.13, 7.3.25, 7.2.31, 7.1.33, 7.0.14 and 5.6.29.
Test script:
---------------
<?php
//Enter your code here, enjoy!
$expectEaster = date_create_from_format('Y-m-d H:i:s', '2020-04-12 12:00:00',
new DateTimeZone('Europe/Berlin'));
$interval = new DateInterval('PT20800M');
$expectEaster->sub($interval);
$expectEaster->add($interval);
echo('easter '.$expectEaster->format('Y-m-d H:i:s')."\n" );
$expectEaster = date_create_from_format('Y-m-d H:i:s', '2020-04-12 12:00:00',
new DateTimeZone('Europe/Berlin'));
$interval = new DateInterval('PT20715M');
$expectEaster->sub($interval);
$expectEaster->add($interval);
echo('easter '.$expectEaster->format('Y-m-d H:i:s')."\n");
$expectEaster = date_create_from_format('Y-m-d H:i:s', '2020-04-12 12:00:00',
new DateTimeZone('Europe/Berlin'));
$interval = new DateInterval('PT20700M');
$expectEaster->sub($interval);
$expectEaster->add($interval);
echo('easter '.$expectEaster->format('Y-m-d H:i:s')."\n");
// Result
// easter 2020-04-12 11:00:00
// easter 2020-04-12 13:00:00
// easter 2020-04-12 12:00:00
Expected result:
----------------
easter 2020-04-12 12:00:00
Actual result:
--------------
'PT20800M': easter 2020-04-12 11:00:00
'PT20715M': easter 2020-04-12 13:00:00
'PT20700M': easter 2020-04-12 12:00:00
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80610&edit=1