php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80610 DateTime calculate wrong with DateInterval
Submitted: 2021-01-09 21:42 UTC Modified: 2021-04-06 19:56 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: info at mobger dot de Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.4.14 OS: Docker / PHPSandbox?
Private report: No CVE-ID: None
 [2021-01-09 21:42 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-01-09 23:23 UTC] 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-10 01:28 UTC] requinix@php.net
-Package: *Calendar problems +Package: Date/time related
 [2021-01-10 01:28 UTC] requinix@php.net
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
 [2021-04-06 19:56 UTC] derick@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2021-04-06 19:56 UTC] derick@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.

Fixed for PHP 8.1.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Nov 28 08:03:16 2021 UTC