Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP DateTime object - Timestamp and Timezones conflict
    text
    copied!<p>From a DateTime object, I am interested in getting the time in different TimeZones. As explained in the <a href="http://www.php.net/manual/en/datetime.settimezone.php" rel="nofollow">DateTime::setTimezone</a> doc, this works pretty well when the DateTime object is created from a string:</p> <pre><code>$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru')); echo $date-&gt;format('Y-m-d H:i:sP') . "\n"; $date-&gt;setTimezone(new DateTimeZone('Pacific/Chatham')); echo $date-&gt;format('Y-m-d H:i:sP') . "\n"; $date-&gt;setTimezone(new DateTimeZone('UTC')); echo $date-&gt;format('Y-m-d H:i:sP') . "\n"; echo $date-&gt;getTimestamp() . "\n"; </code></pre> <p>The above examples will output:<br/> 2000-01-01 00:00:00+12:00<br/> 2000-01-01 01:45:00+13:45<br/> 1999-12-31 12:00:00+00:00<br/> 946641600<br/></p> <p>Now is the interesting part: If we pick up our timestamp, and initiate our DateTime Object with it following the manual instructions.</p> <pre><code>$date2 = new DateTime('@946641600'); $date2-&gt;setTimezone(new DateTimeZone('Pacific/Nauru')); echo $date2-&gt;format('Y-m-d H:i:sP') . "\n"; $date2-&gt;setTimezone(new DateTimeZone('Pacific/Chatham')); echo $date2-&gt;format('Y-m-d H:i:sP') . "\n"; $date2-&gt;setTimezone(new DateTimeZone('UTC')); echo $date2-&gt;format('Y-m-d H:i:sP') . "\n"; echo $date2-&gt;getTimestamp() . "\n"; </code></pre> <p>And here we get: // [edit] humm... Sorry, this output is wrong...<br/> 1999-12-31 12:00:00+00:00<br/> 1999-12-31 12:00:00+00:00<br/> 1999-12-31 12:00:00+00:00<br/> 946641600<br/> <br/> UTC forever !!! We cannot change the timezone anymore !?!<br/> <br/> Is it PHP or is it me ? Version 5.3.15<br/></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload