您需要添加一个检查关闭时间是否属于前一天:
function OperationHours($open, $close){
$status = 'closed';
$nowTime = new DateTime("NOW", new DateTimeZone("GMT"));
$openTime = DateTime::createFromFormat('h:iA', $open, new DateTimeZone("GMT"));
$closeTime = DateTime::createFromFormat('h:iA', $close, new DateTimeZone("GMT"));
// check if the close time is before the opening time
if($closeTime <= $openTime){
$closeTime->add(new DateInterval("P1D"));
}
if ($nowTime > $openTime && $nowTime < $closeTime){
$status = 'open';
}
return $status;
}
echo OperationHours('09:00AM', '11:30AM');
如果使用时区检查日期,请设置
GMT
到定义的时区。否则它会有一个时区差
10:00AM +0300
作为
07:00AM +0000 (GMT)
然后它会回来的
closed
.