Skip to content

Handling of conflicting time zones in the DateTime / DateTimeImmutable constructor #16719

@kixe

Description

@kixe

Description

The following code:

<?php

// given: a time string including timezone (Zulu)
$zuluTimeString = '2024-11-07T11:00:00Z';

$dt = new DateTime($zuluTimeString, new DateTimeZone('Europe/Berlin'));
var_dump($dt->getTimeZone()->getName());
var_dump($dt->format('Y-m-d H:i:sp'));

$dt = new DateTime($zuluTimeString);
$dt->setTimezone(new DateTimeZone('Europe/Berlin'));
var_dump($dt->getTimeZone()->getName());
var_dump($dt->format('Y-m-d H:i:sp'));

$dt = new DateTimeImmutable($zuluTimeString, new DateTimeZone('Europe/Berlin'));
var_dump($dt->getTimeZone()->getName());
var_dump($dt->format('Y-m-d H:i:sp'));

$dt = new DateTimeImmutable($zuluTimeString);
$dt->setTimezone(new DateTimeZone('Europe/Berlin'));
var_dump($dt->getTimeZone()->getName());
var_dump($dt->format('Y-m-d H:i:sp'));

?>

Resulted in this output:

string(1) "Z"
string(20) "2024-11-07 11:00:00Z"
string(13) "Europe/Berlin"
string(25) "2024-11-07 12:00:00+01:00"
string(1) "Z"
string(20) "2024-11-07 11:00:00Z"
string(1) "Z"
string(20) "2024-11-07 11:00:00Z"

But I expected this output instead:

either (always)
string(13) "Europe/Berlin"
string(25) "2024-11-07 12:00:00+01:00"
or Error / Warning due to Timezone conflict in parameters 

PHP Version

PHP 8.3.10

Operating System

MacOS 12.7.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions