开发者

Best GMT time configuration by user | PHP

I was wondering how to do the best user configuration when the user sets his own GMT time, e.g. if a user was in the GMT+3 timezone and the user sets it via the UCP.

What I've done was, every option in the drop-down menu:

<select name="timezone">
    ...
    <option value="-7200">GMT-2</option>
    <option value="-3600">GMT-1</option>
    <option value="0">GMT</option>
    <option value="3600">GMT+1</opti开发者_如何学JAVAon>
    <option value="7200">GMT+2</option>
    ...
</select>

Like that, and when the user chooses it, it will add the value into his user field in the database.

What I'm stuck on is when a post has been created by a user, it uses the time() stamp, and I use the gmdate() function which is this:

while(loop goes here)
{
    $post_time = $row['post_time'] // e.g. 1282165261

    if($row['user_timezone'] > 0)
    {
        $timezone = $post_time-$row['user_timezone'];
    }
    else
    {
        $timezone = $post_time+$row['user_timezone'];
    }
}

Which is outputted with this timestamp format:

gmdate("D jS M Y @ g:iA", $timezone);

But the problem is, if I use something below than GMT, e.g. GMT-1, GMT-6, it changes to +

The question is, what would be the best way to configure timestamps to the user preferred GMT time?


This is pointless.

You shouldn't let the user pick a GMT offset, otherwise:

  • He'll have to change it on every DST transition.
  • When he's in DST, he'll see the wrong times for his timezone for times that occurred outside DST and vice-versa.

What you should do is let him pick an actual timezone (e.g. Europe/Lisbon) from a list.


The problem is in this:

if($row['user_timezone'] > 0)
{
    $timezone = $post_time-$row['user_timezone'];
}
else
{
    $timezone = $post_time+$row['user_timezone'];
}

If you take a good look at it, you'll see that it's equivalent to:

$timezone = $post_time - abs($row['user_timezone']);

You should just replace it with:

$timezone = $post_time - $row['user_timezone'];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜