php-src icon indicating copy to clipboard operation
php-src copied to clipboard

PHP 8.1 Timezone doesn't work as intended

Open Lenny4 opened this issue 4 years ago • 2 comments

Description

PHP 8.1 Timezone doesn't work as intended. offset is wrong in PHP 8.1.

Resulted in this output:

PHP 8.0

$timezone = 'Europe/Paris';
$timestamp = (new \DateTime('2022-02-16 00:00:00'))->getTimestamp();
$result = (new \DateTimeZone($timezone))->getTransitions($timestamp, $timestamp);
// $result['offset'] = 3600

PHP 8.1

$timezone = 'Europe/Paris';
$timestamp = (new \DateTime('2022-02-16 00:00:00'))->getTimestamp();
$result = (new \DateTimeZone($timezone))->getTransitions($timestamp, $timestamp);
// $result['offset'] = 7200

But I expected this output instead:

PHP 8.0

$timezone = 'Europe/Paris';
$timestamp = (new \DateTime('2022-02-16 00:00:00'))->getTimestamp();
$result = (new \DateTimeZone($timezone))->getTransitions($timestamp, $timestamp);
// $result['offset'] = 3600

PHP 8.1

$timezone = 'Europe/Paris';
$timestamp = (new \DateTime('2022-02-16 00:00:00'))->getTimestamp();
$result = (new \DateTimeZone($timezone))->getTransitions($timestamp, $timestamp);
// $result['offset'] = 3600

PHP Version

8.1

Operating System

Ubuntu 20.04.3 LTS

Lenny4 avatar Feb 17 '22 12:02 Lenny4

If you want the offset for a specific point in time I'd recommend using DateTimeImmutable::getOffset() instead.

echo (new DateTimeImmutable('Europe/Paris'))->getOffset();
// 3600

https://3v4l.org/LXVYV

Much more concerning is to me, that PHP8.1 does not seem to return an array of Transitions at all any more.

Code:

$a = new DateTimeImmutable('2022-02-17T12:00:00 Europe/Paris');
$b = $a->add(new Dateinterval('P12M'));
var_Dump($a, $b, (new DateTimezone('Europe/Paris'))->getTransitions($a->getTimestamp(), $b->getTimestamp()));

expected Outcome: an array with three transitions actual Output in PHP8.1: An array with one entry.

See https://3v4l.org/FSAmO

heiglandreas avatar Feb 17 '22 12:02 heiglandreas

Related to https://github.com/php/php-src/issues/7752.

iluuu1994 avatar Feb 17 '22 13:02 iluuu1994