2011-05-28 17 views
0

我需要比較時間,我使用變量來定義時區,當前時間和待比較的時間。PHP與未定義的時區比較時間

我正在使用此腳本根據用戶選擇的內容執行某些操作。用戶可以選擇執行此操作的時間,日期和時區。

如果當前時間大於或等於用戶選擇的時間,則應執行這些操作。

所以我需要比較當前時間和用戶選擇的時間。當前時間由用戶選擇的時區給出。

這是我的代碼。比較工作不正常。它認爲相反的東西比實際上認爲...我需要幫助..可能不使用PHP 5.3或以上。如果功能從版本4到版本5可用,則效果更好

date_default_timezone_set($TimeZone); //all the possible php timezones available can be choosen by the user 
$todaydate = date('Y-m-d'); 
$time_now=mktime(date('G'),date('i'),date('s')); 
$NowisTime=date('G:i:s',$time_now); //the time given by the selected timezone above 
$time = $ris['Time']; //the time defined by the user in the DB 
if($NowisTime >= $time){ 
DO THE ACTIONS 
}else{ 
exit; 

} 

回答

1

如果用戶在數據庫中定義的時間與相同的時區相關。你可以試試這個片斷如下:

$dateTimeZone = new DateTimeZone("Asia/Chongqing"); 
$localTime = new DateTime("now", $dateTimeZone); 
$definedTime = new DateTime("2011-05-29 10:00:00", $dateTimeZone); 
if ($localTime >= $definedTime) { 
    echo "It is about time."; 
} else { 
    // do nothing. 
} 

這將是更好地節省用戶定義的時間Unix Time Stamp