I need some php to output the time in various cities around the world (London, Hong Kong, Bangkok etc), and automatically take account for daylight saving times etc...

Any easy ways to do this? Obviously you can't set the offset manually as it changes from time to time...

I've tried a few readymade scripts but can't get this to work yet (PHP4 and not 5)

    Hmmm, should this work and automatically take account of daylight saving changes in the UK?

    <?
    $timezone0  = 0; // UK (no daylight saving)
    $timezone1  = +4; // Dubai (no daylight saving)
    $timezone2  = +7; // Bangkok (no daylight saving)
    $timezone3  = +8; // Beijing, Hong Kong, Singapore, Shanghai (no daylight saving)
    echo "UK time: " . gmdate("H:i:s", time() + 3600*($timezone0+date("1"))); 
    echo "<br /><br />";
    echo "Dubai time: " . gmdate("H:i:s", time() + 3600*($timezone1+date("0"))); 
    echo "<br /><br />";
    echo "Bangkok time: " . gmdate("H:i:s", time() + 3600*($timezone2+date("0")));
    echo "<br /><br />";
    echo "Beijing, Hong Kong, Singapore, Shanghai time: " . gmdate("H:i:s", time() + 3600*($timezone3+date("0"))); 
    ?>
      <?php
      $now = time();  // use this so all times are to the same second
      $tz = getenv("TZ"); // save local setting so we can reset it later
      putenv("TZ=GMT");
      printf("<p>The time in London is %s</p>\n", date('H:i:s', $now));
      putenv("TZ=Hongkong");
      printf("<p>The time in Hongkong is %s</p>\n", date('H:i:s', $now));
      putenv("TZ=$tz");
      printf("<p>The local server time is %s</p>\n", date('H:i:s', $now));
      ?>
      

        Thanks for this 🙂

        But, it's showing the wrong time for the UK (we are currently +1 due to daylight saving)

        Can you explain how this works, how would I add Dubai for example?

          This should be a bit better. See the list of supported timezones for more info/options.

          <?php
          $now = time();  // use this so all times are to the same second
          $tz = getenv("TZ"); // save local setting so we can reset it later
          putenv("TZ=GMT");
          printf("<p>The GMT/UDT time is %s</p>\n", date('H:i:s', $now));
          putenv("TZ=Europe/London");
          printf("<p>The time in London is %s</p>\n", date('H:i:s', $now));
          putenv("TZ=Asia/Dubai");
          printf("<p>The time in Dubai is %s</p>\n", date('H:i:s', $now));
          putenv("TZ=Asia/Hong_Kong");
          printf("<p>The time in Hong Kong is %s</p>\n", date('H:i:s', $now));
          putenv("TZ=$tz");
          printf("<p>The local server time is %s</p>\n", date('H:i:s', $now));
          ?>
          

            Wow, thank you for this 🙂

            Hours of head-scatching averted!

              Write a Reply...