php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71530 wrong dateTime diff results for 01.03.2016 and 01.03.2017
Submitted: 2016-02-05 09:11 UTC Modified: 2017-04-24 15:57 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: pawel dot zegardlo at bm dot pl Assigned:
Status: Duplicate Package: Date/time related
PHP Version: 5.6.18 OS: windows 8.1 64bit
Private report: No CVE-ID: None
 [2016-02-05 09:11 UTC] pawel dot zegardlo at bm dot pl
Description:
------------
I've tested this on v5.6.15, but in changelog from 5.6.15 to 5.6.18 there is nothing about datetime so this bug seems to be not fixed also in 5.6.18. 

If we change the month in a sample code from march to april, the result is the same for both comparisons (1 year 0 days 0 months). Also if we change the day in sample code from 01 to 02 and stay with march, the result is also the same. But for original code attached, the result is different but should be also the same.

Test script:
---------------
<?php

$start = new DateTime('01.03.2016');
$end = new DateTime('01.03.2017');

$diff = $end->diff($start);

var_dump($start->format('d-m-Y'), $end->format('d-m-Y'), $diff);

$start = new DateTime('02.03.2016');
$end = new DateTime('02.03.2017');

$diff = $end->diff($start);

var_dump($start->format('d-m-Y'), $end->format('d-m-Y'), $diff);


Expected result:
----------------
string '01-03-2016' (length=10)
string '01-03-2017' (length=10)
object(DateInterval)[3]
  public 'y' => int 1
  public 'm' => int 0
  public 'd' => int 0
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 1
  public 'days' => int 365
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0
string '02-03-2016' (length=10)
string '02-03-2017' (length=10)
object(DateInterval)[2]
  public 'y' => int 1
  public 'm' => int 0
  public 'd' => int 0
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 1
  public 'days' => int 365
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0

Actual result:
--------------
string '01-03-2016' (length=10)
string '01-03-2017' (length=10)
object(DateInterval)[3]
  public 'y' => int 0
  public 'm' => int 11
  public 'd' => int 28
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 1
  public 'days' => int 365
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0
string '02-03-2016' (length=10)
string '02-03-2017' (length=10)
object(DateInterval)[2]
  public 'y' => int 1
  public 'm' => int 0
  public 'd' => int 0
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 1
  public 'days' => int 365
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-18 10:04 UTC] ryosuke_i_628 at yahoo dot co dot jp
A related bug is reported here: http://qiita.com/smzk/items/0b8c47dbdb283832a94b
It works on UTC, but doesn't on Asia/Tokyo.

Test script:
---------------
<?php

$patterns = [
    ['2016-01-01', '2016-02-01'],
    ['2016-02-01', '2016-03-01'],
    ['2016-03-01', '2016-04-01'],
    ['2016-02-01', '2016-02-28'],
    ['2016-03-01', '2016-03-31'],
    ['2016-04-01', '2016-04-30'],
];

$timezones = ['UTC', 'Asia/Tokyo'];

foreach ($timezones as $timezone) {
    foreach ($patterns as $pattern) {
        $d1 = new DateTime($pattern[0], new DateTimeZone($timezone));
        $d2 = new DateTime($pattern[1], new DateTimeZone($timezone));
        $diff = $d1->diff($d2);
        printf(
            "There are %d months %2d days between %s and %s (%s)\n",
            $diff->m,
            $diff->d,
            $pattern[0],
            $pattern[1],
            $timezone
        );
    }
    echo "\n";
}


Expected result:
----------------
There are 1 months  0 days between 2016-01-01 and 2016-02-01 (UTC)
There are 1 months  0 days between 2016-02-01 and 2016-03-01 (UTC)
There are 1 months  0 days between 2016-03-01 and 2016-04-01 (UTC)
There are 0 months 27 days between 2016-02-01 and 2016-02-28 (UTC)
There are 0 months 30 days between 2016-03-01 and 2016-03-31 (UTC)
There are 0 months 29 days between 2016-04-01 and 2016-04-30 (UTC)

There are 1 months  0 days between 2016-01-01 and 2016-02-01 (Asia/Tokyo)
There are 1 months  0 days between 2016-02-01 and 2016-03-01 (Asia/Tokyo)
There are 1 months  0 days between 2016-03-01 and 2016-04-01 (Asia/Tokyo)
There are 0 months 27 days between 2016-02-01 and 2016-02-28 (Asia/Tokyo)
There are 0 months 30 days between 2016-03-01 and 2016-03-31 (Asia/Tokyo)
There are 0 months 29 days between 2016-04-01 and 2016-04-30 (Asia/Tokyo)

Actual result:
--------------
There are 1 months  0 days between 2016-01-01 and 2016-02-01 (UTC)
There are 1 months  0 days between 2016-02-01 and 2016-03-01 (UTC)
There are 1 months  0 days between 2016-03-01 and 2016-04-01 (UTC)
There are 0 months 27 days between 2016-02-01 and 2016-02-28 (UTC)
There are 0 months 30 days between 2016-03-01 and 2016-03-31 (UTC)
There are 0 months 29 days between 2016-04-01 and 2016-04-30 (UTC)

There are 1 months  0 days between 2016-01-01 and 2016-02-01 (Asia/Tokyo)
There are 0 months 29 days between 2016-02-01 and 2016-03-01 (Asia/Tokyo)
There are 1 months  2 days between 2016-03-01 and 2016-04-01 (Asia/Tokyo)
There are 0 months 27 days between 2016-02-01 and 2016-02-28 (Asia/Tokyo)
There are 1 months  1 days between 2016-03-01 and 2016-03-31 (Asia/Tokyo)
There are 0 months 29 days between 2016-04-01 and 2016-04-30 (Asia/Tokyo)
 [2017-04-24 15:57 UTC] heiglandreas@php.net
-Status: Open +Status: Duplicate
 [2017-04-24 15:57 UTC] heiglandreas@php.net
Looks like this is related to https://bugs.php.net/bug.php?id=52480
 [2017-09-16 05:51 UTC] ryosuke_i_628 at yahoo dot co dot jp
This bug is still available!
 [2017-09-16 09:29 UTC] spam2 at rhsoft dot net
and that won't change because PHP 5.6 is End-Of-Life
 [2017-09-16 11:44 UTC] ryosuke_i_628 at yahoo dot co dot jp
No, this is available on all versions.

Online PHP editor | output for rH2BC
https://3v4l.org/rH2BC
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 04:01:29 2024 UTC