Skip to content

DateTimeImmutable::createFromTimestamp() uses UTC timezone #5651

Description

@MircoBabin

Affected page

https://www.php.net/manual/en/datetimeimmutable.createfromtimestamp.php

Current issue

It is not documented that createFromTimestamp() sets the timezone to UTC. Which is unexpected, because the absence of a timezone normally uses date_default_timezone_get().

Suggested improvement

  1. Document the unexpected UTC timezone.

  2. Document the (very ugly) workaround

DateTimeImmutable::createFromTimestamp($someTimestamp)->setTimezone(new DateTimeZone(date_default_timezone_get()));

Additional context (optional)

I created a test at https://3v4l.org/d3Rq1#v8.5.7 which demonstrates the current behaviour.

<?php

if (!date_default_timezone_set('Europe/Amsterdam')) {
    throw new Exception('Timezone Europe/Amsterdam does not exist?');
}

echo 'Default timezone: '.date_default_timezone_get().PHP_EOL;
var_dump(new DateTimeZone(date_default_timezone_get()));

$someTimestamp = strtotime('2026-07-03 14:22:17');

echo '--- date'.PHP_EOL;
echo date('Y-m-d H:i:s', $someTimestamp).PHP_EOL;

echo '--- DateTimeImmutable::createFromTimestamp() defaults to UTC timezone'.PHP_EOL;
$dt = DateTimeImmutable::createFromTimestamp($someTimestamp);
echo $dt->format('Y-m-d H:i:s').PHP_EOL;
var_dump($dt->getTimezone());

echo '--- DateTimeImmutable::createFromTimestamp() workaround for current timezone'.PHP_EOL;
$dt = DateTimeImmutable::createFromTimestamp($someTimestamp)->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo $dt->format('Y-m-d H:i:s').PHP_EOL;
var_dump($dt->getTimezone());

outputs:

Default timezone: Europe/Amsterdam
object(DateTimeZone)#1 (2) {
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Europe/Amsterdam"
}
--- date
2026-07-03 14:22:17
--- DateTimeImmutable::createFromTimestamp() defaults to UTC timezone
2026-07-03 12:22:17                          !!!!!!!! NOTICE THE 2 HOURS DIFFERENCE  !!!!!!!!
object(DateTimeZone)#2 (2) {
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+00:00"
}
--- DateTimeImmutable::createFromTimestamp() workaround for current timezone
2026-07-03 14:22:17
object(DateTimeZone)#1 (2) {
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Europe/Amsterdam"
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions