2012-04-17 87 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

相關問題