Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthormista3
    • CommentTimeOct 26th 2006
     permalink
    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?

    I've tried a few readymade scripts but can't get this to work yet (PHP4 and not 5)
    • CommentAuthormista3
    • CommentTimeOct 26th 2006 edited
     permalink
    Hmmm, I did find this which supposedly accounts for winter/summer time...

    <?
    $timezone = -5; //(GMT -5:00) EST (U.S. & Canada)
    echo gmdate("Y/m/j H:i:s", time() + 3600*($timezone+date("I")));
    ?>
    • CommentAuthorMatt
    • CommentTimeOct 26th 2006
     permalink
    This actually is a bit of an issue, I think what my co-worker did was base the output off the offset... like +3 for eastern time or whatever. I don't think it accounts for time zone changes though. You can... maybe... scrape the output of http://whattimeisit.com/cgitime.exe?Mode=FullList one a day, store the offsets for each place, and then use the time function and add an hour or whatever accordingly.
    • CommentAuthormista3
    • CommentTimeOct 26th 2006
     permalink
    Hmm complicated.

    The time's are feeding into Flash clocks so need to auto-update, I'm not going to change offsets manually ever!

    It seems a bit of a complicated issue yes... can't get a definite solution anywhere so far...
    • CommentAuthoreasement
    • CommentTimeOct 26th 2006
     permalink
    I use this for getting the GMT on the server I'm on:

    //getting timezone variables so polling date is correct
    $longOffset = shell_exec('ls -l /etc/localtime');

    if (ereg("GMT+",$longOffset)) {
    $gmt = split("GMT",$longOffset);
    } else if (ereg("GMT-",$longOffset)) {
    $gmt = split("GMT",$longOffset);
    }
    if (isset($gmt)){
    $gmtNum = $gmt[1];
    $gmtVar = trim("Etc/GMT".$gmt[1]);
    }


    Here's the dropdown I use to set the timezeone:
    <?php
    function checkOffset($checkVal,$gmtOffSet) {
    if ($checkVal == $gmtOffSet) {
    echo "selected=\"selected\"";
    }
    }
    ?>
    <select name="timezone">
    <option value="+12" <?php checkOffset(+12,$gmt[1]); ?>>GMT+12: IDLE/NZST</option>
    <option value="+11" <?php checkOffset(+11,$gmt[1]); ?>>GMT+11:</option>
    <option value="+10" <?php checkOffset(+10,$gmt[1]); ?>>GMT+10: GST</option>
    <option value="+9" <?php checkOffset(+9,$gmt[1]); ?>>GMT+09: JST</option>
    <option value="+8" <?php checkOffset(+8,$gmt[1]); ?>>GMT+08: CCT</option>
    <option value="+7" <?php checkOffset(+7,$gmt[1]); ?>>GMT+07:</option>
    <option value="+6" <?php checkOffset(+6,$gmt[1]); ?>>GMT+06:</option>
    <option value="+5" <?php checkOffset(+5,$gmt[1]); ?>>GMT+05:</option>
    <option value="+4" <?php checkOffset(+4,$gmt[1]); ?>>GMT+04:</option>
    <option value="+3" <?php checkOffset(+3,$gmt[1]); ?>>GMT+02: EET</option>
    <option value="+2" <?php checkOffset(+2,$gmt[1]); ?>>GMT+03: BT</option>
    <option value="+1" <?php checkOffset(+1,$gmt[1]); ?>>GMT+01: CET</option>
    <option value="+0" <?php checkOffset(0,$gmt[1]); ?>>GMT: GMT</option>
    <option value="-1" <?php checkOffset(-1,$gmt[1]); ?>>GMT-01: WAT</option>
    <option value="-2" <?php checkOffset(-2,$gmt[1]); ?>>GMT-02: AT</option>
    <option value="-3" <?php checkOffset(-3,$gmt[1]); ?>>GMT-03:</option>
    <option value="-4" <?php checkOffset(-4,$gmt[1]); ?>>GMT-04: AST</option>
    <option value="-5" <?php checkOffset(-5,$gmt[1]); ?>>GMT-05: EST</option>
    <option value="-6" <?php checkOffset(-6,$gmt[1]); ?>>GMT-06: CST</option>
    <option value="-7" <?php checkOffset(-7,$gmt[1]); ?>>GMT-07: MST</option>
    <option value="-8" <?php checkOffset(-8,$gmt[1]); ?>>GMT-08: PST</option>
    <option value="-9" <?php checkOffset(-9,$gmt[1]); ?>>GMT-09: YST</option>
    <option value="-10" <?php checkOffset(-10,$gmt[1]); ?>>GMT-10: AHST</option>
    <option value="-11" <?php checkOffset(-11,$gmt[1]); ?>>GMT-11: NT</option>
    <option value="-12" <?php checkOffset(-12,$gmt[1]); ?>>GMT-12: IDLW</option>
    </select>


    Maybe there's something in there that you can use.
    • CommentAuthoreasement
    • CommentTimeOct 26th 2006
     permalink
    I think there may be a better way to get the current TimeZone using getenv("TZ"); I'm not 100% sure, but they talk about setting it with putenv on this page:
    <a href="http://us3.php.net/putenv" target="_blank">http://us3.php.net/putenv</a>
    • CommentAuthormista3
    • CommentTimeOct 28th 2006 edited
     permalink
    I got some help with this on PHPBuilder.com.... you can use putenv like this pretty successfully:

    <?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));
    ?>


    Also a list of supported timezones at http://www.php.net/manual/en/timezones.php :)
Add your comments
    Username Password
  • Format comments as (Help)