[doc-en] master: Document the timezone of DateTime(Immutable)::createFromTimestamp() (#6074)

From: Date: Fri, 13 Nov 2026 10:16:31 +0000
Subject: [doc-en] master: Document the timezone of DateTime(Immutable)::createFromTimestamp() (#6074)
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-23949@lists.php.net to get a copy of this message
Author: Asish Kumar (officialasishkumar) Committer: GitHub (web-flow) Pusher: lacatoire Date: 2026-11-13T11:16:28+01:00 Commit: https://github.com/php/doc-en/commit/69ce9dc901258987257483724c4b7d79c501c66c Raw diff: https://github.com/php/doc-en/commit/69ce9dc901258987257483724c4b7d79c501c66c.diff Document the timezone of DateTime(Immutable)::createFromTimestamp() (#6074) 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 #5651 Changed paths: M reference/datetime/datetime/createfromtimestamp.xml M reference/datetime/datetimeimmutable/createfromtimestamp.xml Diff: diff --git a/reference/datetime/datetime/createfromtimestamp.xml b/reference/datetime/datetime/createfromtimestamp.xml index 5d24b80c83b3..7b0b28cf8c89 100644 --- a/reference/datetime/datetime/createfromtimestamp.xml +++ b/reference/datetime/datetime/createfromtimestamp.xml @@ -15,6 +15,15 @@ <simpara> Creates an instance from a Unix timestamp. </simpara> + <simpara> + The timezone of the returned object is always the UTC offset + <literal>+00:00</literal>, as with + <code>new DateTime('@' . $timestamp)</code>. + The default timezone, as returned by + <function>date_default_timezone_get</function>, is not used. + To get the date and time in another timezone, call + <methodname>DateTime::setTimezone</methodname> on the returned object. + </simpara> </refsect1> <refsect1 role="parameters"> @@ -65,6 +74,29 @@ echo $date->format('Y-m-d H:i:s.u'); <screen> <![CDATA[ 1970-01-01 00:02:03.456789 +]]> + </screen> + </example> + <example xml:id="datetime.createfromtimestamp.example.timezone"> + <title>Converting the result to the default timezone</title> + <programlisting role="php"> +<![CDATA[ +<?php +date_default_timezone_set('Europe/Amsterdam'); + +$date = DateTime::createFromTimestamp(1700000000); +echo $date->format('Y-m-d H:i:s P e'), "\n"; + +$date->setTimezone(new DateTimeZone(date_default_timezone_get())); +echo $date->format('Y-m-d H:i:s P e'), "\n"; +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +2023-11-14 22:13:20 +00:00 +00:00 +2023-11-14 23:13:20 +01:00 Europe/Amsterdam ]]> </screen> </example> diff --git a/reference/datetime/datetimeimmutable/createfromtimestamp.xml b/reference/datetime/datetimeimmutable/createfromtimestamp.xml index d6cc26ea29fd..86beb0a2d291 100644 --- a/reference/datetime/datetimeimmutable/createfromtimestamp.xml +++ b/reference/datetime/datetimeimmutable/createfromtimestamp.xml @@ -15,6 +15,15 @@ <simpara> Creates an instance from a Unix timestamp. </simpara> + <simpara> + The timezone of the returned object is always the UTC offset + <literal>+00:00</literal>, as with + <code>new DateTimeImmutable('@' . $timestamp)</code>. + The default timezone, as returned by + <function>date_default_timezone_get</function>, is not used. + To get the date and time in another timezone, call + <methodname>DateTimeImmutable::setTimezone</methodname> on the returned object. + </simpara> </refsect1> <refsect1 role="parameters"> @@ -65,6 +74,29 @@ echo $date->format('Y-m-d H:i:s.u'); <screen> <![CDATA[ 1970-01-01 00:02:03.456789 +]]> + </screen> + </example> + <example xml:id="datetimeimmutable.createfromtimestamp.example.timezone"> + <title>Converting the result to the default timezone</title> + <programlisting role="php"> +<![CDATA[ +<?php +date_default_timezone_set('Europe/Amsterdam'); + +$date = DateTimeImmutable::createFromTimestamp(1700000000); +echo $date->format('Y-m-d H:i:s P e'), "\n"; + +$date = $date->setTimezone(new DateTimeZone(date_default_timezone_get())); +echo $date->format('Y-m-d H:i:s P e'), "\n"; +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +2023-11-14 22:13:20 +00:00 +00:00 +2023-11-14 23:13:20 +01:00 Europe/Amsterdam ]]> </screen> </example>

« previous php.doc.cvs (#23949) next »