PHP 商店营业时间晚上

标签 php date time

因此,我尝试使用 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/

相关文章:

php - 如何仅在验证后在 php 中上传 .mp3 文件,我的 .wav 文件很容易上传

date - 如何使用moment将字符串日期转换为unix格式

php - 以毫秒为单位将 MYSQL 日期时间四舍五入到最早的 15 分钟间隔 (PHP)

sql - Laravel Carbon 使用 sameDay() 查询日期

time - 推荐个人使用的基于 Web 的时间/任务管理解决方案?

c# - 员工时钟计算

php - 为 DateTime 准备 PHP 代码

php - 向文本/普通电子邮件添加换行符 (\r\n\r\n )

php - 如何从 friend 表中获取 friend 列表以及我 friend 的 friend 数量

date - 配置单元未检测到时间戳格式