Skip to content

DateTime diff returns wrong sign on day count when using a timezone (Regression) #9880

Closed
@neildaniels

Description

@neildaniels

Description

The following code:

https://3v4l.org/7biFY

<?php

ini_set('date.timezone', 'America/Los_Angeles');

$nowTime = new DateTime();
$nowTime->setTimestamp(1667416695);

$dateTime = new DateTime();
$dateTime->setTimestamp(1671904800);
$dateTime->setTimezone(new DateTimeZone('America/New_York'));

echo $dateTime->diff($nowTime)->format('%r%a');

Resulted in this output:

51

But I expected this output instead:

-51

This code works as expected prior to 8.1.10.

If you comment out the $dateTime->setTimezone(new DateTimeZone('America/New_York')); line, the code works as expected on all versions.

PHP Version

PHP 8.1.12

Operating System

Alpine 3.16

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions