对不起,对于一个愚蠢的问题,这肯定是一个非常简单的答案,但我的大脑冻结了!
我想要一个函数,如果当前时间在开始和结束时间之间,它将返回true,这样应用程序就不会运行。实际上是一个"安静的时间";
用户可以在24小时时钟中设置开始时间和结束时间:
$start_time = "0300";
$end_time = "0900";
使用以下方法几乎有效:
function isQuietTime($start,$end)
{
if((date("Hm") >= $start) && (date("Hm") <= $end)) {return TRUE;} else {return FALSE;}
}
但是,如果开始时间是2300,结束时间是0600,当前时间是0300,该怎么办?上述函数将返回false。当开始时间在当天结束之前,而结束时间在第二天时,就会出现问题。我该如何让它发挥作用?
谢谢!
function fixedIsQuietTime($start, $end)
{
if ($start < $end)
return isQuietTime($start, $end);
else
return ! isQuietTime($end, $start);
}
我建议使用UNIX时间。因此,编写一个函数将输入时间转换为UNIX时间。类似这样的东西:
function to_unix_time($string){
// Something like this...
$unix_time = mktime();
return $unix_time;
}
function isQuietTime($start, $end){
$now = time();
$stime = to_unix_time($start);
$etime = to_unix_time($end);
if($now > $stime && $now < $etime){
//its quiet time!
return true;
}
}