phplaraveldatetimephp-8laravel-12

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


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

It seems like a bug in PHP. Am I right? Can somebody confirm my investigation?


Solution

  •  $row->created = (new DateTime('@' . $row->created))->setTimezone(new DateTimeZone(config('app.timezone')));
    

    Not a bug,the timezone is intentionally ignored when using @-style timestamps.