我试图根据他们提供的开放和关闭时间显示业务运营状态,但即使我在网上关注了几个例子,它似乎也无法运作。 我知道这个问题可能是用不同的方法提出来的,有人可以协助我解决这个问题,因为即使业务开放,下面的功能也显示已关闭。
我的php功能
#application
答案 0 :(得分:1)
您需要检查结束时间是否属于其他日期:
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
。