2016-02-13 84 views
0

我想如何檢查給定時間是否在兩個時間範圍內。 例如:22:00 < 22:30,這裏情況變得錯誤。提前致謝。檢查輸入時間是否存在於兩個時間範圍內

$time1="11:00"; 
$time2="22:30"; 
$currenttime=date("22:00"); 

if($time1 - $currenttime < 0 && $currenttime - $time2 < 0) { 
    echo "Lies in range"; 
} else { 
    echo "Not lies in range"; 
} 
+1

顯示您的代碼來獲得妥善的解決辦法.... – Nikunj

+0

$時間1 = 「11:00」; $ time2 =「22:30」; $ currenttime = date(「22:00」); //當前時間 if($ time1 - $ currenttime <0 && $ currenttime - $ time2 <0) { echo「Lies in range」; } else { echo「不在範圍內」; } – Joe

+0

diff1 = $ time1 - $ currenttime; diff2 = $ currenttime - $ time2; if(diff1 <0 && diff2 <0){echo「in range in」; } else {echo「不在範圍內」; } 試試這個.... – Nikunj

回答

1

下面的代碼應該工作:

$time1 = strtotime("11:00"); 
$time2 = strtotime("22:30"); 
$currenttime = strtotime("22:00"); 
if ($time1 - $currenttime < 0 && $currenttime - $time2 < 0) 
{ 
    echo "Lies in range"; 
} 
else 
{ 
    echo "Not lies in range"; 
} 
+0

感謝您的解決方案。它效果很好。你救了我 。 – Joe

+1

這樣你不能檢查時間是否在包括午夜的時間間隔內:'$ time1 = strtotime(「22:00」);''和'$ time2 = strtotime(「06:00」);'。 – maxhb

+0

$ time1 = strtotime(「2016年2月13日22:00」); $ time2 = strtotime(「14 feb 2016 06:30」); $ currenttime = strtotime(「14 feb 2016 05:59」); 添加日期可以解決這個問題。 謝謝你的支持! –

相關問題