2015-08-22 26 views
1

我目前有這種格式'00:00'的開始和結束時間,例如H:我。我目前正試圖從時間中刪除4個小時,然後確定新時間是否是前一天,例如, 00:00之前。PHP:直接比較H:i中的兩次,並確定新時間是否在00:00之前

一個例子:

$oldStart = '00:00'; 
$oldStart = date("H:i", strtotime($oldStart)); 
$oldEnd = '00:30'; 
$oldEnd = date("H:i", strtotime($oldEnd)); 
$newStart = date("H:i", strtotime('-4 hours', $oldStart)); 
$newEnd = date("H:i", strtotime('-4 hours', $oldEnd)); 
$dayStart = date("H:i", strtotime('00:00')); 
if($newStart < $dayStart) { 
    echo 'day before<br>'; 
} else { 
    echo 'current day<br>'; 
} 

我期待這隨着時間的現在有4個小時的後面,但它是呼應「當天」'前一天呼應。我看不到我要去哪裏錯了...

回答

0

只要使用此:

$old = "4:00"; 
$dt = new DateTime($old); 
$dt->modify("-4 hours"); 
$new = $dt->format("Y-m-d"); 
if(date("Y-m-d", strtotime($old)) > $new){ 
    echo "Day before"; 
} else { 
    echo "Same day"; 
} 
相關問題