Note that there are some explanatory texts on larger screens.

plurals
  1. POAdjusting time zone in PHP with DateTime / DateTimeZone
    primarykey
    data
    text
    <p>There's a lot of info on doing time zone adjustments in PHP, but I haven't found an answer for specifically what I want to do due to all the noise.</p> <p>Given a time in one timezone, I want to convert it to the time in another timezone. </p> <p><a href="https://stackoverflow.com/questions/1074290/calculate-the-difference-between-date-times-in-php">This</a> is essentially what I want to do, but I need to be able to do it using only the built-in PHP libs, not PEAR Date.</p> <p>This is what I've been doing, but it seems to always give me the offset relative to GMT:</p> <pre><code>$los_angeles_time_zone = new DateTimeZone('America/Los_Angeles'); $hawaii_time_zone = new DateTimeZone('Pacific/Honolulu'); $date_time_los_angeles = new DateTime('2009-09-18 05:00:00', $los_angeles_time_zone); printf("LA Time: %s&lt;br/&gt;", $date_time_los_angeles-&gt;format(DATE_ATOM)); $time_offset = $hawaii_time_zone-&gt;getOffset($date_time_los_angeles); printf("Offset: %s&lt;br/&gt;", $time_offset); </code></pre> <p>This is the output:</p> <blockquote> <p>LA Time: 2009-09-18T05:00:00-07:00<br/> Offset: -36000</p> </blockquote> <p>I was expecting 3 hours (10800 seconds). but the '-7:00' thing tells me it's keeping everything relative to GMT, which maybe explains why it's giving me the "absolute" offset.</p> <p>How do I just get the offset between the two timezones without all of this GMT hoohah?</p> <p>Thanks.</p> <p>UPDATE:</p> <p>I occured to me that I could do this and get what I want:</p> <pre><code> $date_time_los_angeles = new DateTime('2009-09-18 05:00:00', $los_angeles_time_zone); printf("LA Time: %s&lt;br/&gt;", $date_time_los_angeles-&gt;format(DATE_ATOM)); $date_time_hawaii = new DateTime('2009-09-18 05:00:00', $hawaii_time_zone); printf("Hawaii Time: %s&lt;br/&gt;", $date_time_hawaii-&gt;format(DATE_ATOM)); $time_offset = $los_angeles_time_zone-&gt;getOffset($date_time_los_angeles) - $hawaii_time_zone-&gt;getOffset($date_time_los_angeles); printf("Offset: %s&lt;br/&gt;", $time_offset); </code></pre> <p>But it feels awkward to me. Anyone know a cleaner way to do it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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