Edit report at https://bugs.php.net/bug.php?id=81458&edit=1
ID: 81458
Updated by: cmb@php.net
Reported by: kylekatarnls at gmail dot com
Summary: Regression in PHP 8.1: Incorrect difference after
timezone change
Status: Open
Type: Bug
Package: Date/time related
PHP Version: 8.1Git-2021-09-18 (Git)
Block user comment: N
Private report: N
New Comment:
See <https://3v4l.org/RiYdl>.
While there is obviously a behavioral change as of PHP 8.1.0, it
seems to me that the new behavior of ::$days is correct. In the
given test script there is a difference of 20 hours, and that is
less than a day. Unfortunately, the documentation is not
particularly clear whether this is to be expected, and why ::$d
has a different result.
Derick, can you please clarify?
Previous Comments:
------------------------------------------------------------------------
[2021-10-21 10:48:16] cmb@php.net
The following pull request has been associated:
Patch Name: Bug #81458: Add test
On GitHub: https://github.com/php/php-src/pull/7601
Patch: https://github.com/php/php-src/pull/7601.patch
------------------------------------------------------------------------
[2021-10-11 07:57:26] alec at alec dot pl
This is a bug and a regression. I hope it gets more attention before the final release.
From a user perspective it is obvious that the compared DateTime objects needs to be "converted
internally" to the same timezone to do the calculations.
------------------------------------------------------------------------
[2021-09-19 22:20:39] antonino dot spampinato86 at gmail dot com
sorry for my english, if you use DateTime :: diff the value is a number sequence. From php 8 it
incorrectly calculates the moment, i.e. the date string is the first parameter of DateTime between
two dates, the result is 20 instead of 24 hours (one day). It is a bug but also php <8 which
converts the date string to UTC, ie it loses or adds hours that expand per day. php <8 previously
2018-07-01 20:00:00 America / Toronto in diff was converted to 2018-07-02 00:00:00 UTC instead of
using moment. Wait to hear from maintainer @dereck. i am a simple user i don't work for php :)
<?php
$first = (new DateTime('2018-07-01 00:00:00.000000 America/Toronto'))
->setTimezone(new DateTimeZone('UTC')); // 2018-07-01 04:00:00 UTC
$second = new DateTime('2018-07-02 00:00:00.000000 America/Toronto');
var_dump($first->diff($second)->days); //2018-07-01 04:00:00 UTC - 2018-07-02 00:00
Anerica/Toronto = 20 hours
var_dump($first->diff($second)->d);
var_dump($first->diff($second)->h);
------------------------------------------------------------------------
[2021-09-19 19:33:04] kylekatarnls at gmail dot com
Again, I may understand the point about 1 being not the correct result, and that
false would be the correct one.
But why 0? It's non sense.
I know it's very tempting when facing a regression to blame users expectations or previous
versions to be wrong. That looks to me like a confirmation bias.
Please triple-check this int(0) value in an objective way. How can it be correct?
If you "can't" calculate, then return false please, so we can detect it
when checking an interval.
Thanks.
------------------------------------------------------------------------
[2021-09-19 15:15:53] antonino dot spampinato86 at gmail dot com
Carbon or php <8.1 does not use the right code, a date must be calculated from its own moment.
This speech is true only if between the same, otherwise offset first normalizes the date and then
you convert to UTC or look at how many DST or ST between two dates with different offset and
currently the php code does not provide this. It's arithmetic. Perhaps it would make sense to
change into fatal error if calculating two dates with different time zones.
Human 2018-07-01 04:00:00 UTC to 2018-07-02 00:00:00 America/Toronto bug moment is 20 hours for
diff, if converter to 2018-07-02 04:00:00 is 24 hours (maybe one day if without transitions DST or
ST).
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=81458
--
Edit this bug report at https://bugs.php.net/bug.php?id=81458&edit=1