php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77097 DateTime::diff gives wrong diff when the actual diff is less than 1 second
Submitted: 2018-11-03 11:59 UTC Modified: 2018-12-05 11:57 UTC
Votes:4
Avg. Score:4.2 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:0 (0.0%)
From: vishnunn at ymail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.3.0RC4 OS: CentOS Linux 7.5.1804
Private report: No CVE-ID: None
 [2018-11-03 11:59 UTC] vishnunn at ymail dot com
Description:
------------
---
From manual page: https://php.net/datetime.diff
---


Test script:
---------------
$now = new DateTime('2018-11-03 11:34:20.781751');
$ago = new DateTime('2018-11-03 11:34:20.000000');
$diff = $now->diff($ago);
var_dump($diff);

Expected result:
----------------
On PHPv7.2.11 it gives the correct output:
Date/time in $now: 2018-11-03 11:34:20.781751
Date/time in $ago: 2018-11-03 11:34:20.000000
object(DateInterval)#574 (16) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(0)
  ["d"]=>
  int(0)
  ["h"]=>
  int(0)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["f"]=>
  float(-0.781751)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(0)
  ["days"]=>
  int(0)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}

Actual result:
--------------
On PHPv7.3RC4 it give this output which is wrong:
Date/time in $now: 2018-11-03 11:50:50.826351
Date/time in $ago: 2018-11-03 11:50:50.000000
object(DateInterval)#574 (16) {
  ["y"]=>
  int(-1)
  ["m"]=>
  int(11)
  ["d"]=>
  int(30)
  ["h"]=>
  int(23)
  ["i"]=>
  int(59)
  ["s"]=>
  int(59)
  ["f"]=>
  float(0.173649)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(0)
  ["days"]=>
  int(0)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-11-03 12:21 UTC] cmb@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: derick
 [2018-11-03 12:21 UTC] cmb@php.net
I can confirm a behavioral change, albeit I get a slightly
different result with PHP-7.3 (@9afce01), where 'f' is
float(0.218249).  This would basically still be somehow correct,
but if I request an $absolute result, I still get an identical
result, which is certainly not correct.

Derick, could you please have a look at this?
 [2018-11-09 06:04 UTC] sakari dot laine at verkkokauppa dot com
Affects also 7.2.12
 [2018-11-22 14:31 UTC] wyatt at wyday dot com
We can confirm this bug exists on PHP 7.3.0 RC5 (for our development machines) and PHP 7.2.12 for our production machines. This is a showstopper for us. We've had to rollback to 7.2.11 on our production machines and we'll be unable to upgrade to any new version until this bug is fixed.

It looks like it's related to the latest timelib upgrade. It looks like you should revert that change in the next version until all the edge cases are solved.
 [2018-11-22 14:42 UTC] wyatt at wyday dot com
I've also reported this bug on the timelib repository with more details: https://github.com/derickr/timelib/issues/51
 [2018-11-28 20:35 UTC] derick@php.net
Automatic comment on behalf of github@derickrethans.nl
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a3f2871b8ee6975e70685bca803d5499e540b803
Log: Fixed bug #77097 (DateTime::diff gives wrong diff when the actual diff is less than 1 second) by upgrading to timelib 2017.09
 [2018-11-28 20:35 UTC] derick@php.net
-Status: Verified +Status: Closed
 [2018-12-03 14:01 UTC] cmb@php.net
Automatic comment on behalf of github@derickrethans.nl
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ea302f8b085466da1f417070f966ccce1e5ea9d2
Log: Fixed bug #77097 (DateTime::diff gives wrong diff when the actual diff is less than 1 second)
 [2018-12-05 09:31 UTC] daszwarc at olx dot pl
PHP 7.2.12 is also affected
 [2018-12-05 11:57 UTC] derick@php.net
Yes. It should get fixed in 7.2.14 and 7.3.0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC