Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does php datetime diff depend on time zones?

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?
like image 294
Csongor Halmai Avatar asked Nov 01 '16 05:11

Csongor Halmai


People also ask

How get DateTime Diff in PHP?

You can simply use datetime diff and format for calculating difference. <? php $datetime1 = new DateTime('2009-10-11 12:12:00'); $datetime2 = new DateTime('2009-10-13 10:12:00'); $interval = $datetime1->diff($datetime2); echo $interval->format('%Y-%m-%d %H:%i:%s'); ?>

How does PHP date work?

The date function in PHP is used to format the timestamp into a human desired format. The timestamp is the number of seconds between the current time and 1st January, 1970 00:00:00 GMT. It is also known as the UNIX timestamp. The default time zone can also be set programmatically using PHP scripts.

What is timezone PHP?

The date_default_timezone_set() function sets the default timezone used by all date/time functions in the script.

How can we convert the time zones using PHP?

php $time='6:02'; $dt = new DateTime($time, new DateTimeZone('America/New_York')); //echo $dt->format('Y-m-d H:i:s') . PHP_EOL; $dt->setTimezone(new DateTimeZone('Asia/Kolkata')); echo $dt->format('H:i') .


1 Answers

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.

like image 198
Ruslan Osmanov Avatar answered Nov 11 '22 19:11

Ruslan Osmanov