Bug #79452 [Opn->Csd]: DateTime::diff() generates months differently between time zones

From: Date: Tue, 06 Apr 2021 19:56:13 +0000
Subject: Bug #79452 [Opn->Csd]: DateTime::diff() generates months differently between time zones
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233265@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79452&edit=1 ID: 79452 Updated by: derick@php.net Reported by: corey dot taylor dot fl at gmail dot com Summary: DateTime::diff() generates months differently between time zones -Status: Open +Status: Closed Type: Bug Package: Date/time related PHP Version: 7.4.4 -Assigned To: +Assigned To: derick Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2020-05-06 14:53:15] antonino dot spampinato86 at gmail dot com correction July 1st is the first month while it should be the second month. ------------------------------------------------------------------------ [2020-05-06 14:38:13] antonino dot spampinato86 at gmail dot com the error lies in the evaluation of +1 month from a date that begins at the end of May 31 since php calculates from the current month it takes initially 31 days on July 1 is the second month while it should be the third month. It should only calculate the time spent in months without days. <?php date_default_timezone_set('America/New_York'); $from = new DateTime('2019-06-01'); $to = new DateTime('2019-10-01'); var_dump($from->diff($to)->m); date_default_timezone_set('Asia/Tokyo'); $from = new DateTime('2019-06-01'); $from->setTimezone(new DateTimeZone('UTC')); // 2019-05-31 15:00:00 $to = new DateTime('2019-10-01'); $to->setTimezone(new DateTimeZone('UTC')); // 2019-09-30 15:00:00 // 4 month for php 2019-10-01 15:00:00 var_dump(($from->diff($to)->m), $from, $to); ------------------------------------------------------------------------ [2020-04-07 05:48:48] 1515888956 at qq dot com calc error because timelib function apply time to gmttime. so the different month is the gmt different month l am alread motify this bug. but l don't not sure is right https://github.com/derickr/timelib/pull/78 ------------------------------------------------------------------------ [2020-04-06 11:51:34] corey dot taylor dot fl at gmail dot com I can see why php calculates the month value different for those UTC DateTime instances. Is there no way to get a consistent calendar operation in different time zones? The only alternative here seems to be mimicking the same "date" in UTC for users in other time zones when calculating calendar changes. ------------------------------------------------------------------------ [2020-04-06 08:14:06] cmb@php.net Before the actual diff is calculated, the DateTime objects are converted to UTC; doing this in PHP shows the problem[1]. [1] <https://3v4l.org/gBtjX#v720> ------------------------------------------------------------------------ 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=79452 -- Edit this bug report at https://bugs.php.net/bug.php?id=79452&edit=1

« previous php.bugs (#233265) next »