Why does php datetime diff depend on time zones?

2020-02-14 01:32发布

问题:

See the following code:

function printDiff($tz) {
    $d1 = new DateTime("2015-06-01", new DateTimeZone($tz));
    $d2 = new DateTime("2015-07-01", new DateTimeZone($tz));
    $diff = $d1->diff($d2);
    print($diff->format("Year: %Y Month: %M Day: %D"). PHP_EOL);
}
printDiff("UTC");
printDiff("Australia/Melbourne");

The result is:

Year: 00 Month: 01 Day: 00
Year: 00 Month: 00 Day: 30

Questions:

  • How can the difference between the same day of two adjacent months (1st of June and July) be different than 1 month?
  • Why does the computation mode depend on which timezone I use if there is no DST between the given days, there is no leap year, there is nothing special?
  • Where can I find the exact description of the algorithm how the difference is computed?

回答1:

The date extension stores the time values in GMT. The GMT offset is stored separately. It is applied at the late phases of calculations, for correction.

Particularly, DateTime::diff internally calls timelib_diff function, which calculates difference between two dates, applies the DST correction, then normalizes its internal structures, in that order.

When both dates are in UTC, the function compares the following:

  • Timestamp=1433116800, year=2015, month=6, day=1, hour=0, minute=0, second=0
  • Timestamp=1435708800, year=2015, month=7, day=1, hour=0, minute=0, second=0

It subtracts years, months, days, hours, minutes, and seconds correspondingly. The difference is exactly one month. Therefore, the internal structures are not modified after normalization.

When both dates are in Australia/Melbourne, the function compares the following:

  • Timestamp=1433080800, year=2015, month=5, day=31, hour=14, minute=0, second=0
  • Timestamp=1435672800, year=2015, month=6, day=30, hour=14, minute=0, second=0

Both dates are obtained by subtracting 10 hours (the timezone offset without DST correction). As we have seen, the DST correction is applied after subtraction of the time values, if needed (it is not needed, in particular). The difference before normalization is:

0 years, 1 month, -1 day, 0 hours, 0 minutes, 0 seconds

Since we have an offset between the months, normalization function computes the difference as

0 years, 0 months, 30 days, 0 hours, 0 minutes, 0 seconds

The DateInterval::format method is straightforward. It just substitutes the placeholders to the members of timelib_rel_time structure. That's why we get 1 month for UTC, and 30 days for Australia/Melbourne.

The a format character

That looks somewhat unreliable. However, there is a days member in the timelib_rel_time structure, which always equals to the difference in days. The value is available via the a format character:

function printDiff($tz) {
  $d1 = new DateTime("2015-06-01", new DateTimeZone($tz));
  $d2 = new DateTime("2015-07-01", new DateTimeZone($tz));
  $diff = $d1->diff($d2);
  print($diff->format("Year: %Y Month: %M Day: %D days: %a"). PHP_EOL);
}

printDiff("UTC");
printDiff("Australia/Melbourne");

Output

Year: 00 Month: 01 Day: 00 days: 30
Year: 00 Month: 00 Day: 30 days: 30

P.S.

The values in this answer are obtained with the help of TIMELIB_DEBUG macro.