php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74524 Date diff is bad calculated, in same time zone
Submitted: 2017-05-01 20:59 UTC Modified: 2021-04-06 19:54 UTC
Votes:3
Avg. Score:3.3 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: kalita dot michal at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.1.4 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
36 + 23 = ?
Subscribe to this entry?

 
 [2017-05-01 20:59 UTC] kalita dot michal at gmail dot com
Description:
------------
Calculated diff is 7 months, 14 days and -1 hour.

It doen't make sense, why it is not 7 months, 13 days and 23 hours.

This bug is in versions 5.4.45, 5.6.30, 7 and newest. (https://3v4l.org/6OOuG)


Test script:
---------------
$a = new DateTime("2017-11-17 22:05:26.000000");
$b = new DateTime("2017-04-03 22:29:15.079459");

$diff = $a->diff($b);

return $diff->h;

Expected result:
----------------
23

Actual result:
--------------
-1

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-02 12:22 UTC] andrew dot nester dot dev at gmail dot com
thanks for reporting the issue!
I've been trying to reproduce it on latest PHP7.1 version (7.1.6-dev) and seems it's fixed already.
Anyway, I've added test to check that it works (see my PR)
 [2017-05-02 12:33 UTC] derick@php.net
Andrew, it might look fixed for you because you have a different timezone with different DST rules configured.
 [2017-05-02 20:06 UTC] andrew dot nester dot dev at gmail dot com
Thanks Derick!
gonna rework my pull request
 [2017-05-03 07:18 UTC] andrew dot nester dot dev at gmail dot com
Just added reworked pull request to this issue. Here is the link https://github.com/php/php-src/pull/2506
 [2017-05-06 19:02 UTC] heiglandreas@php.net
This is a DST issue as can be seen in https://3v4l.org/9AWOI

The default Timezone of 3v4l.org is Europe/Amsterdam which has DST and therefore has the issue!
 [2017-10-30 11:26 UTC] onda204 at gmail dot com
Hi,
I detected the following issue that may have to do with present Bug.
Using diff with dates that are on range of summer / winter time change, the function diff on element $interval->h gives result -1

Tested width date interval:
27-10-2017
15.00 H

29-10-2017
14.00 H

Portuguese regional settings
 [2017-12-01 16:46 UTC] miloslav dot hula at gmail dot com
Hi,
probably another example of bug: https://3v4l.org/S7rGP

In the Europe/Prague timezone, there was a 1 hour switch back on 29th Oct 2017 from 03:00 to 02:00 (from CEST to CET).

When run on 3v4l.org with EOL versions checked, you can notice that behaviour has been correct in 5.3.0-5.4.23, 5.5.0-5.5.7. In the PHP5 5.4.24 and 5.5.8 change log is fixed bug #66060 (https://bugs.php.net/bug.php?id=66060).
 [2020-10-22 08:54 UTC] steve dot caillault at gmail dot com
I a similar problem with the PHP version 7.4.7 :

$currentDateString = '2020-10-21 22:22:59';
$targetDateString = '2020-10-25 21:30:00';

$timezone = new \DateTimeZone('Europe/Paris');
$currentDateTime = new \DateTime($currentDateString, $timezone);
$targetDateTime = new \DateTime($targetDateString, $timezone);

$dateInterval = $currentDateTime->diff($targetDateTime, TRUE);

echo $dateInterval->h;

$dateInterval->h value is -1, while the value should be 23.

I have the problem on Windows and UNIX.
 [2021-04-06 19:54 UTC] derick@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2021-04-06 19:54 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-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 11:01:27 2024 UTC