使用strtotime并考虑时区


Using strtotime and taking timezone into consideration

我有一个用PHP运行的系统,正在使用CodeIgniter(如果有一个特定于CI的答案,我也会很高兴)。

我经常需要弄清楚日期的时间戳,比如"本周四"或"本周一"。我目前使用strtotime("this Thursday"),它满足了我的要求。

我在EST有一台服务器。现在是2012年2月24日星期五凌晨01:00(凌晨1:00),纽约。

我有一个用户指定他的时区为太平洋时间。现在是旧金山2012年2月23日星期四晚上22:00(晚上10点)。

我使用strtotime("this Thursday")。由于我的服务器处于EST,它会返回2012年3月1日的时间戳。我想考虑一下用户的时区。对于我的用户来说,"本周四"应该在2012年2月23日返回。

我可以使用strtotime("this Thursday")吗?如果可以,我将如何指定目标时区?

如果没有,你建议用什么方法在特定时区确定"本周四"的日期。

manual for strtotime直接链接到date_default_timezone_set。我会把它当作一个暗示来使用它。

我刚刚发现strtotime需要一个$now参数。我可以将$now参数指定为用户时区中的当前时间,strtotime会返回我需要的时间。

这是从php站点获取的字符串

<?php
date_default_timezone_set('Asia/Shanghai');
$first_day_of_month = date('Y-m',time()) . '-01 00:00:01';
$t = strtotime($first_day_of_month);
print_r(array(
            date('Y-m',$t),
            date('Y-m',strtotime('- 1 month',$t)),
            date('Y-m',strtotime('- 2 month',$t)),
));
?>