我需要检查当前时间是否在timerange.最简单的例子time_end> TIME_START:
if time(6,0) <= now.time() <= time(12,00): print '1'
但是当用户在结束时间小于开始时间时输入时间范围时,会出现故障,例如, “23:00 – 06:00”.像“00:00”这样的时间将在这个范围内.大约5年前我写了这个PHP函数:
function checkInterval($start, $end)
{
$dt = date("H:i:s");
$tstart = explode(":", $start);
$tend = explode(":", $end);
$tnow = explode(":", $dt);
if (!$tstart[2])
$tstart[2] = 0;
if (!$tend[2])
$tend[2] = 0;
$tstart = $tstart[0]*60*60 + $tstart[1]*60 + $tstart[2];
$tend = $tend[0]*60*60 + $tend[1]*60 + $tend[2];
$tnow = $tnow[0]*60*60 + $tnow[1]*60 + $tnow[2];
if ($tend < $tstart)
{
if ($tend - $tnow > 0 && $tnow > $tstart)
return true;
else if ($tnow - $tstart > 0 && $tnow > $tend)
return true;
else if ($tend > $tnow && $tend < $tstart && $tstart > $tnow)
return true;
else return false;
} else
{
if ($tstart < $tnow && $tend > $tnow)
return true;
else
return false;
}
现在我需要做同样的事情,但我想让它好看.那么,我应该使用什么算法来确定当前时间’00:00’是否处于相反的范围内. [’23:00′,’01:00′]?