Skip to content

Document the timezone of DateTime(Immutable)::createFromTimestamp() - #6074

Merged
lacatoire merged 1 commit into
php:masterfrom
officialasishkumar:datetime-createfromtimestamp-timezone
Nov 13, 2026
Merged

lacatoire merged 1 commit into
php:masterfrom
officialasishkumar:datetime-createfromtimestamp-timezone

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

DateTime::createFromTimestamp() and DateTimeImmutable::createFromTimestamp() always return an object with the UTC offset +00:00 as its timezone, the same as new DateTime('@' . $timestamp). The default timezone is never used: php_date_initialize_from_ts_long() sets TIMELIB_ZONETYPE_OFFSET with no offset. Most other ways of creating a date use the default timezone, and the pages didn't mention this, so the result looked wrong, as reported in the issue.

This adds to both pages:

  • a paragraph in the description saying the timezone is always +00:00, that the default timezone from date_default_timezone_get() is not used, and that setTimezone() converts the result;
  • an example that does that conversion. The DateTime version calls setTimezone() in place, and the DateTimeImmutable version assigns the returned object.

I ran both examples on PHP 8.4 and they print the output shown. configure.php validates and docbook-cs reports no violations.

Fixes #5651

Both methods always return an object whose timezone is the UTC offset
+00:00, like new DateTime('@' . $timestamp), whatever the default
timezone is (php_date_initialize_from_ts_long() sets
TIMELIB_ZONETYPE_OFFSET with no offset). This surprises users who
expect the default timezone, as with the constructor and most other
ways to create a date, and the pages did not mention it.

Say so in the description of both pages and add an example that
converts the result to the default timezone with setTimezone(). The
example was run on PHP 8.4 and prints the output shown.

Fixes php#5651

@lacatoire lacatoire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for documenting the UTC timezone of createFromTimestamp()!

@lacatoire
lacatoire merged commit 69ce9dc into php:master Nov 13, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DateTimeImmutable::createFromTimestamp() uses UTC timezone

2 participants