2016-02-13 83 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”); 添加日期可以解决这个问题。 谢谢你的支持! –

相关问题