php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78452 diff makes wrong in hour for Asia/Tehran
Submitted: 2019-08-24 17:20 UTC Modified: 2021-04-06 19:56 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: hamedzarei dot 7232 at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.3.11 OS: linux
Private report: No CVE-ID: None
 [2019-08-24 17:20 UTC] hamedzarei dot 7232 at gmail dot com
Description:
------------
Diff between 2 DateTimes makes wrong in hour and create -1
I test some cases and think is about setting timezone

Test script:
---------------
date_default_timezone_set('Asia/Tehran');
$date1 = new \DateTime('2019-09-24 11:47:24');
$date2 = new \DateTime('2019-08-21 12:47:24');

var_dump($date1->diff($date2));

Expected result:
----------------
object(DateInterval)#3 (16) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(1)
  ["d"]=>
  int(2)
  ["h"]=>
  int(23)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(1)
  ["days"]=>
  int(33)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}


Actual result:
--------------
object(DateInterval)#3 (16) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(1)
  ["d"]=>
  int(3)
  ["h"]=>
  int(-1)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(1)
  ["days"]=>
  int(33)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}

Patches

diff_timezone_patch_hamed_01 (last revision 2019-08-24 17:21 UTC by hamedzarei dot 7232 at gmail dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-24 17:21 UTC] hamedzarei dot 7232 at gmail dot com
The following patch has been added/updated:

Patch Name: diff_timezone_patch_hamed_01
Revision:   1566667287
URL:        https://bugs.php.net/patch-display.php?bug=78452&patch=diff_timezone_patch_hamed_01&revision=1566667287
 [2019-08-24 17:37 UTC] requinix@php.net
-Summary: diff makes worng in hour +Summary: diff makes wrong in hour for Asia/Tehran -Status: Open +Status: Verified
 [2019-08-28 16:33 UTC] joe at manvscode dot com
Tehran is in Asia though?
 [2019-08-31 14:36 UTC] zhangwenchao001 at gmail dot com
I think this is a problem in daylight saving time.

Test script:
---------------
date_default_timezone_set('Asia/Tehran');
$date1 = new \DateTime('2019-09-21 11:47:24');
$date2 = new \DateTime('2019-08-18 12:47:24');

var_dump($date1->diff($date2));

--------------
object(DateInterval)#3 (16) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(1)
  ["d"]=>
  int(2)
  ["h"]=>
  int(23)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(1)
  ["days"]=>
  int(33)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}
 [2019-08-31 14:42 UTC] zhangwenchao001 at gmail dot com
<?php

It seems need to fix daylight saving time

date_default_timezone_set('America/New_York');
$date1 = new \DateTime('2019-11-05 11:47:24');
$date2 = new \DateTime('2019-10-02 12:47:24');

var_dump($date1->diff($date2));

----------------
object(DateInterval)#3 (16) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(1)
  ["d"]=>
  int(3)
  ["h"]=>
  int(-1)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(1)
  ["days"]=>
  int(33)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}
 [2019-11-07 11:14 UTC] nikic@php.net
-PHP Version: 7.4.0beta4 +PHP Version: 7.3.11
 [2020-01-20 17:14 UTC] girgias@php.net
-Assigned To: +Assigned To: derick
 [2021-01-15 13:54 UTC] antonino dot spampinato86 at gmail dot com
It is a documentation problem, the time -1 indicates that it is to be subtracted from the day not covered by the month (php differs from human arithmetic and can mistakenly believe that 30 days is a month instead it takes 31 days or vice versa https://www.php.net/manual/en/datetime.examples-arithmetic.php ), both the outputs are on the same side.

https://3v4l.org/4JSG1
 [2021-04-06 19:56 UTC] derick@php.net
-Status: Verified +Status: Closed
 [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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC