2016-08-03 53 views
0

我在確定如何驗證時區是否超過特定時間(本地時區)方面存在一些問題。檢查時區是否已經過了一定時間

因此,舉例來說,如果在倫敦的時間已經過去了18:00:00

$tz = new DateTimeZone('Europe/London'); 
$datetime1->setTimezone($tz); // calculates with new TZ now 

if ($datetime1->format('H:i:s') >= strtotime('18:00:00')) { 

    echo "time has passed"; 
} else { 
    echo "time has NOT passed"; 
} 

的問題,這是的strtotime('18:00:00)似乎是使用服務器時間。 如果我回復strtotime('18:00:00');將返回1470247200這是自1970年以來的秒數,但這不會是另一個時區的下午6點,例如America/New_York,在撰寫本文時尚未通過。

任何想法如何做到這一點?

感謝,

+0

你有答案嗎?如果是,請確保標記正確的答案。 –

回答

0

我認爲這應該工作:

if ($datetime1->format('H:i:s') >= '18:00:00') { 

左側是一個字符串,每個組件包含前導零,所以你可以做與右側字符串比較。

(假設你考慮第二天的午夜到沒有「通過」 18:00:00)

0

使用DateTime自己的比較功能,因爲它包含的時區支持:

$tz = new DateTimeZone('Europe/London'); 
$datetime1->setTimezone($tz); // calculates with new TZ now 

$datetime2 = new \DateTime('18:00:00', $tz); 

if ($datetime1 >= sdatetime2) {  
    echo "time has passed"; 
} else { 
    echo "time has NOT passed"; 
} 
相關問題