Skip to content

Datetime(CreateFromTimestamp/CreateFromFromat(U)) - no timezone #18143

@dwalck

Description

@dwalck

Description

The following code:

<?php

 echo date_default_timezone_get();
 echo PHP_EOL;

$dt = new \DateTimeImmutable();
echo $dt->getTimezone()->getName();
echo PHP_EOL;

$dt1 = new \DateTimeImmutable()->setTimestamp(1742892008);
echo $dt1->getTimezone()->getName();
echo PHP_EOL;

$dt2 = \DateTimeImmutable::createFromTimestamp(1742892008);
echo $dt2->getTimezone()->getName();
echo PHP_EOL;

$dt3 = \DateTimeImmutable::createFromFormat('U', '1742892008');
echo $dt3->getTimezone()->getName();
echo PHP_EOL;

Resulted in this output:

UTCUTCUTC
+00:00
+00:00

But I expected this output instead:

UTCUTCUTC
UTC
UTC

PHP Version

8.4.5

Operating System

No response

CreateFromFormat with non-timestamp value seems to working properly:

\DateTimeImmutable::createFromFormat('H:i:s d.m.Y', '09:30:00 25.03.2025') 

will create object with valid timezone.

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