|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2021-09-18 19:56 UTC] kylekatarnls at gmail dot com
Description:
------------
In PHP 8.0 diff()->days returned the total number of days between dates even if one of them were in UTC and not the other
In PHP 8.1 (since beta3 I think) ->d is still OK but ->days is now 0
Test script:
---------------
<?php
$first = (new DateTime('2018-07-01 00:00:00.000000 America/Toronto'))
->setTimezone(new DateTimeZone('UTC'));
$second = new DateTime('2018-07-02 00:00:00.000000 America/Toronto');
var_dump($first->diff($second)->days);
var_dump($first->diff($second)->d);
Expected result:
----------------
int(1)
int(1)
Actual result:
--------------
int(0)
int(1)
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 03:00:02 2025 UTC |
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);After explaining the difference between the new diff behavior from php 8.1 the algorithm has also changed unfortunately SetTimeZone uses the server time and not the DateTime diff object does not recognize +00: 00 but UTC for this reason forces UTC in the code. $first = (new DateTime('2018-07-01 00:00:00.000000 America/Toronto')); $first = (new DateTime('@' . $first->format('U'))); $first = (new DateTime($first->format('Y-m-d H:i:s'), new DateTimeZone('UTC'))); // ->setTimezone(new DateTimeZone('UTC')); $second = new DateTime('2018-07-02 00:00:00.000000 America/Toronto'); $result = $first->diff($second); var_dump($first); var_dump($second); var_dump('UTC ' . ((new DateTime('@' . $second->format('U')))->format('Y-m-d H:i:s'))); var_dump($result->days); var_dump($result->d); var_dump($result);Hello derick, FYI I now get this returning me days = 0 using master branch, while if I properly understood your specs it should be 1: ``` date_default_timezone_set('UTC'); $a = new DateTime('2018-12-01 00:00'); $b = new DateTime('2018-12-02 00:01'); var_dump($a->diff($b)); ``` So it sounds now there is a regression when both are in UTC.Hi, not sure if this is related. DateTime diff for 'year' in different timezones returns -1 after PHP 8.1.10 instead of 0. Is this a known bug? $utc = new DateTimeZone('UTC'); $london = new DateTimeZone('Europe/London'); $test1 = new \DateTime('2018-01-31', $utc); $test2 = new \DateTime('2018-04-08', $london); $dateDiff = $test1->diff($test2); echo $dateDiff->y;