根据服务器 UTC 偏移量和用户 UTC 偏移量更改当前用户时区 [英] Changing current user timezone based on server UTC offset and user UTC offset
问题描述
我正在用 php 编写 Twitter 网络服务.当用户登录时,我收到此节点:
im writing a twitter web service in php. When a user signs in, i receive this node:
<utc_offset>-18000</utc_offset>
我必须更改脚本的时区以适应用户的实际时区.我为此找到的唯一 php 函数是:date_default_timezone_set($timezone_identifier) 但它不会让我使用 -18000 作为 $timezone_identifier 参数.
I have to change the script's timezone so that it adapts to the user's real timezone. The only php function i have found for this is: date_default_timezone_set($timezone_identifier) but it won't let me use -18000 as a the $timezone_identifier parameter.
那么,我如何根据两个值更改当前用户时区:服务器 UTC 偏移量和用户 UTC 偏移量
So, how can i change the current user timezone based on two values: Server UTC offset and User UTC offset
顺便说一句,这是我获取服务器 UTC 偏移值的方式:
BTW, this is how i'm getting the server UTC offset value:
$this_tz_str = date_default_timezone_get();
$this_tz = new DateTimeZone($this_tz_str);
$now = new DateTime("now", $this_tz);
$offset = $this_tz->getOffset($now);
有什么想法吗?谢谢!
推荐答案
获取当前服务器时间
date_default_timezone_set(date_default_timezone_get());
echo date('Y-m-d H:i:s', time());
欧洲/巴黎的输出(我的服务器设置;UTC+2)
Output for Europe/Paris (my server settings; UTC+2)
2011-04-12 20:39:43
通过偏移量获取用户的时间
To get user's time by offset
$user_offset = '-18000';
date_default_timezone_set('UTC');
$diff = "$user_offset seconds";
if ((substr($diff,0,1) != '+') && (substr($diff,0,1) != '-')) $diff = '+' . $diff;
$usertime = strtotime($diff, time());
echo date('Y-m-d H:i:s', $usertime);
输出 UTC-5(厄瓜多尔 -> 基多时间无夏令时),php 时区标识符 'America/Guayaquil'
.
Output UTC-5 (Ecuador -> Quito time NO DST), php timezone identifier 'America/Guayaquil'
.
2011-04-12 13:39:43
PHP.net 手册:
PHP.net manual:
时区偏移量(以秒为单位).偏移量对于UTC以西的时区总是负数,对于 UTC 以东的人来说是总是积极的.(-43200 通过50400)
Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. (-43200 through 50400)
这篇关于根据服务器 UTC 偏移量和用户 UTC 偏移量更改当前用户时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!