2012-04-17 32 views
1

我的整个网站是:日期时间不会改变临时

date_default_timezone_set('Europe/London'); 

用户可以选择自己的时区,并在数据库中保存为4.0(迪拜)

现在我想有这么用户选择的时区对网站上存在的倒计时有影响。倒计时看起来是这样的:

$today_date = new DateTime('now'); // now 
    $final_date = new DateTime($date); // a date in the future 
    $interval = $today_date->diff($final_date); // find the difference 

    $time_left = $interval->format('starting in %d days, %h hours and %i minutes'); // display it 

在此之上,我做以下内容:

 $userGMT = $user->get_gmt(); 
     date_default_timezone_set($userGMT); 

不工作正确的。欧洲/伦敦时区仍然倒计时,而不是我选择的那个。

我试过做echo time('H:i:s');并可以看到上面已经受到影响的时间,它显示了我选择的时区的时间。

这样的工作,但dateTime('现在');犯规?

+0

你尝试'新的日期时间( '现在',新DateTimeZone(“欧洲/伦敦))'? – 2012-04-17 22:45:50

+0

是的,没有为我工作 – Karem 2012-04-18 07:07:25

+0

你打开'error_reporting(E_ALL)'和'ini_set('display_errors','1')'来捕获所有可能的错误(只在开发当然)?所有时区是否正确安装?在控制台中用'locale -a'检查。 – 2012-04-18 07:12:24

回答

0

你想要做的是将添加到你的currentTime,然后减去你的服务器时间。因此,请在几秒钟内获得时间,然后添加$userGMT*60*60 - $yourTimeZone*60*60

0

生成未来日期时,需要明确适用的时区。这表明间隔是独立于主机的时区:

<?php 
$date = '2012-04-19'; // date in the future 
$user_tz = new DateTimeZone('Asia/Dubai'); 
foreach (array('Europe/London', 'America/Los_Angeles') as $tzname) 
{ 
    date_default_timezone_set($tzname); 
    $today_date = new DateTime('now'); // now 
    $final_date = new DateTime($date, $user_tz); // future date with explicit tz 
    $interval = $today_date->diff($final_date); // find the difference 
    $time_left = $interval->format('start in %d days, %h hours and %i minutes'); 
    echo "$tzname: $time_left\n"; 
} 

输出:

Europe/London: start in 0 days, 11 hours and 53 minutes 
America/Los_Angeles: start in 0 days, 11 hours and 53 minutes 
+0

所以你说我想做什么是不可能的,因为使用主机的时区,不管是什么? – Karem 2012-04-20 08:20:31

+0

@Karem:完全没有。我建议的修复不受主机时区的影响。 – wallyk 2012-04-20 08:28:24

+0

但在您的输出中,它在欧洲/伦敦和美国/ los_angeles中输出相同的小时/分钟,这是不对的?美国/洛杉矶格林威治标准时间8应显示在你的例子3小时左右和53分钟 – Karem 2012-04-21 09:12:39