Skip to content

PHP new DateTime() with unix timestamp ignores Timezone for application and also explicit Timezone in constructor #18870

Closed as not planned
@camohub

Description

@camohub

Description

I have an object which have created property like unix timestamp integer. I try to convert this property to DateTime object and supposed the PHP will set up correct Timezone according application settings. Application is Laravel 12 which setting for timezone is correct as I see in a log file. Look at the code:

Log::info((new DateTimeZone(config('app.timezone')))->getName());  // Europe/Bratislava
$row->created = (new DateTime('@' . $row->created), new DateTimeZone(config('app.timezone')));
Log::info($row->created->getTimezone()->getName());  // +00:00  !!!!!

The DateTime constructor completelly ignores second argument. Next code works fine:

Log::info((new DateTimeZone(config('app.timezone')))->getName());  // Europe/Bratislava
$row->created = (new DateTime('@' . $row->created))->setTimezone(new  DateTimeZone(config('app.timezone')));
Log::info($row->created->getTimezone()->getName());  // Europe/Bratislava

PHP Version

PHP 8.2

Operating System

Windows 10

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