PHP商店夜间营业时间

PHP商店夜间营业时间,php,date,time,Php,Date,Time,因此,我正试图用PHP为一家网店创建一条打开和关闭的消息,访问者可以在这里订购 storehours当前以以下格式存储: 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天的截止日期,而实

因此,我正试图用PHP为一家网店创建一条打开和关闭的消息,访问者可以在这里订购

storehours当前以以下格式存储:

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';
}
$open\u close=爆炸('-',$hours);
//当前日期和时间
$now=新日期时间();
//克隆当前日期时间并明确将时间设置为打开时间
$open=clone$now;
$open_time=explode(“:”,$open_close[0]);
调用_user_func_数组([$open,'setTime'],$open_time);
//在关门时间也要这样做
$close_time=explode(“:”,$open_close[1]);
$close=clone$now;
调用_user_func_数组([$close,'setTime'],$close_time);
//如果“关闭”大于“关闭”,则“关闭”必须是第二天,除非它是数据输入错误,但我们无法真正防止这种情况发生,因此假设它是好的,并将“关闭”的日期时间修改为第二天
如果($open>$close){
$close->modify(“+1天”);
}
//现在进行一个基本比较,看看$now是否在$open和$close之间
如果($now<$close&$now>=$open){
回声“开放”;
}否则{
回声“关闭”;
}
我也没有提到你需要将一周中的几天映射到当天,我想你已经做到了。如果不是这样,我想你需要做一些调整,但这应该能让你得到一个准确的比较。

我认为这是可行的。
我将打开和关闭时间转换为unix值

如果“关闭”在“打开”之前,则以秒为单位添加一天

现在我有了一个unix值,即它今天打开和关闭的时间,我需要做的就是与时间()进行比较

$otime=“16:15”;
$ctime=“00:50”;
$open=标准时间(日期(“Y-m-d”)。$otime);
$close=STROTIME(日期)(“Y-m-d”)。$ctime;
如果($close=$open&&time()<$close){
呼应“开放”;
}否则{
回声“关闭”;
}

您可以将第四天分为两部分:第四天12:00-23:59、第五天00:00-02:50和12:00-23:00。使用DateTime类可能更容易处理这一点,因为它有一些内置的跨度检查()用最简单的术语来说,您需要检查滚动到第二天的成对的日期和时间。实际执行检查的代码是什么样的?
call\u user\u func\u array
?只需使用解包<代码>$open->setTime(…$open\u time)
所以我用输入尝试了您的代码:$hours=“16:15-00:50”。虽然它应该说“打开”,但它说的是关闭@TTS沙箱有美国太平洋时区,目前有
14:22
@TTS这两种代码在功能上是相似的,但一些人认为strotime将在20年后停止工作。我不太确定。我认为某些版本的PHP会解决32位的问题。但这是代码之间的主要区别。Datetime没有32位问题。如果你愿意,请随意选择。
$otime="16:15";
$ctime="00:50";
$open = strtotime(date("Y-m-d ").$otime);
$close = strtotime(date("Y-m-d ").$ctime);

If($close<$open) $close += 3600*24;

If(time() >= $open && time() < $close){
    Echo "open";
}Else{
    Echo "closed";
}