1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00
Files
archived-php-src/ext/date/tests/gh20764.phpt
Niels Dossche ee26417b58 Fix timezone offset with seconds losing precision
There are two issues:
1. The 'e' formatter doesn't output the seconds of the timezone even if
   it has seconds.
2. var_dump(), (array) cast, serialization, ... don't include the
   timezone second offset in the output. This means that, for example,
   serializing and then unserializing a date object loses the seconds of
   the timezone. This can be observed by comparing the output of
   getTimezone() for `$dt` vs the unserialized object in the provided test.

Closes GH-20764.
2026-02-05 18:38:54 +01:00

54 lines
1.0 KiB
PHP

--TEST--
GH-20764 (Timezone offset with seconds loses precision)
--FILE--
<?php
$timezones = [
'+03:00:30',
'-03:00:30',
];
foreach ($timezones as $timezone) {
echo "--- Testing timezone $timezone ---\n";
$tz = new DateTimeZone($timezone);
$dt = new DateTimeImmutable('2025-04-01', $tz);
var_dump($dt->format('e'));
var_dump($dt);
var_dump(unserialize(serialize($dt))->getTimezone());
}
?>
--EXPECTF--
--- Testing timezone +03:00:30 ---
string(9) "+03:00:30"
object(DateTimeImmutable)#%d (3) {
["date"]=>
string(26) "2025-04-01 00:00:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(9) "+03:00:30"
}
object(DateTimeZone)#%d (2) {
["timezone_type"]=>
int(1)
["timezone"]=>
string(9) "+03:00:30"
}
--- Testing timezone -03:00:30 ---
string(9) "-03:00:30"
object(DateTimeImmutable)#%d (3) {
["date"]=>
string(26) "2025-04-01 00:00:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(9) "-03:00:30"
}
object(DateTimeZone)#%d (2) {
["timezone_type"]=>
int(1)
["timezone"]=>
string(9) "-03:00:30"
}