Wrong result from DateTimeImmutable::diff
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Description
If you execute this code, it produces an extremely weird result. The first DTI is 11 minutes after what we’re ->diff() ’ing against, but when in the America/Los_Angeles TZ, it produces a very strange result. Converting the first DTI to UTC then produces the expected difference of 11 minutes.
//Difference of -1 year, 11 months, 31 days, etc.
<?php
$invalid_result = (new \DateTimeImmutable('2024-01-31 17:00:00', new \DateTimeZone('America/Los_Angeles')))
->diff(new \DateTimeImmutable('2024-02-01 00:49:00'));
//Difference of 11 minutes
$correct_result = (new \DateTimeImmutable('2024-01-31 17:00:00', new \DateTimeZone('America/Los_Angeles')))
->setTimezone(new \DateTimeZone('UTC'))
->diff(new \DateTimeImmutable('2024-02-01 00:49:00'));
var_dump($invalid_result);
var_dump($correct_result);
$invalid_result Resulted in this output:
object(DateInterval)#3 (10) {
["y"]=>
int(-1)
["m"]=>
int(11)
["d"]=>
int(31)
["h"]=>
int(0)
["i"]=>
int(11)
["s"]=>
int(0)
["f"]=>
float(0)
["invert"]=>
int(1)
["days"]=>
int(0)
["from_string"]=>
bool(false)
}
But I expected this output instead:
object(DateInterval)#2 (10) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(0)
["h"]=>
int(0)
["i"]=>
int(11)
["s"]=>
int(0)
["f"]=>
float(0)
["invert"]=>
int(1)
["days"]=>
int(0)
["from_string"]=>
bool(false)
}
PHP Version
PHP 8.1
Operating System
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the supplied PHP 8.1 script and comparing DateTimeImmutable::diff with and without setTimezone('UTC'). Trace the DateTimeImmutable::diff entry point and timezone handling, then add coverage showing that the America/Los_Angeles case produces the expected 11-minute interval.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100