因此,我尝试使用 PHP 为访问者可以订购的网上商店创建打开和关闭消息。
商店营业时间当前以以下格式存储:
1 12:00-22:00
2 12:00-23:00
3 closed-closed
4 12:00-02:50
5 12:00-23:00
etc..
前三天是这样。打印关闭消息非常容易,因为您只需将字符串与日期(“Hi”)进行比较并检查当前时间是否在商店营业时间之间。但到了第四天,要通宵达旦,我就开始挣扎了。当时间达到 00:00 时,它当然会检查第 5 天的截止日期,而实际上,它应该检查晚上是否开放。
有可靠的方法让它发挥作用吗?
最佳答案
我没有考虑您的封闭-封闭
场景,但这只是一个简单的检查。
$open_close = explode('-', $hours);
// current date and time
$now = new DateTime();
// clone the current datetime and explicity set the time to the open time
$open = clone $now;
$open_time = explode(':', $open_close[0]);
call_user_func_array([$open, 'setTime'], $open_time);
// do the same for the close time
$close_time = explode(':', $open_close[1]);
$close = clone $now;
call_user_func_array([$close, 'setTime'], $close_time);
// if close is greater than close, the close must be the next day unless its a data entry error, but we cant really prevent that, so assume its good and modify the date time for close to be the next day
if ($open > $close) {
$close->modify('+1 day');
}
// now a basic comparison to see if $now is between $open and $close
if ($now < $close && $now >= $open) {
echo 'OPEN';
} else {
echo 'Closed';
}
我也没有解决您将一周中的几天映射到当天的需求,我假设您已经做到了这一点。如果没有,那么我认为您将需要进行一些调整,但这总体上应该可以让您进行准确的比较。
关于PHP 商店营业时间晚上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47998448/