我的MySQL数据库中的打开和关闭时间如下:
opening_time_Sunday 10:30
closing_time_Sunday 00:00
opening_time_Monday 09:30
closing_time_Monday 01:30
opening_time_Tuesday 09:30
closing_time_Tuesday 01:30
opening_time_Wednesday 09:30
closing_time_Wednesday 01:30
opening_time_Thursday 09:30
closing_time_Thursday 01:30
opening_time_Friday 09:30
closing_time_Friday 23:00
opening_time_Saturday 10:30
closing_time_Saturday 00:00
我遇到的问题是试图显示商店的开门和关门时间,例如星期一,如果它经过了午夜,它将开始读取星期二的时间,并显示商店关门了,即使它打算一直营业到01:30 am。
我了解为什么会发生这种情况,但不确定使用哪种逻辑(例如,星期一)直到截止日期为止,这是我当前的功能:
public static function setting($setting)
{
$config = Settings::where('setting', $setting)->first();
if ($config) {
return $config->value;
}
}
public static function Today() {
$today = date('l');
$start = self::setting('opening_time_' . $today); // Select value from settings table
$end = self::setting('closing_time_' . $today); // Select value from settings table
return $start . " - " . $end;
}
答案 0 :(得分:2)
要动态确定要显示的日期,您需要通过检查今天的结束日期是否大于当前时间,对上一个日期的结束时间执行消除处理。
但是,由于关闭时间不包含星期几,因此您还必须验证关闭时间为am
,以避免误报。
public static function Today() {
$currentDate = new \DateTimeImmutable;
$priorDate = $currentDate->sub(new \DateTimeInterval('P1D'));
$yesterday = $priorDate->format('l');
$priorClosing = self::setting('closing_time_' . $yesterday);
$closeDate = $currentDate->setTime(...explode(':', $priorClosing));
if ($closeDate->format('a') === 'am' && $currentDate < $closeDate) {
//check if the closing has not occurred yet
$currentDate = $priorDate;
}
$today = $currentDate->format('l');
$start = self::setting('opening_time_' . $today);
$end = self::setting('closing_time_' . $today);
return $start . " - " . $end;
}
结果:
Day | 00:15:00 | 23:00:00
Monday | Monday: 09:30 - 01:30 | Monday: 09:30 - 01:30
Tuesday | Monday: 09:30 - 01:30 | Tuesday: 09:30 - 01:30
Wednesday | Tuesday: 09:30 - 01:30 | Wednesday: 09:30 - 01:30
Thursday | Wednesday: 09:30 - 01:30 | Thursday: 09:30 - 01:30
Friday | Thursday: 09:30 - 01:30 | Friday: 09:30 - 23:00
Saturday | Saturday: 10:30 - 00:00 | Saturday: 10:30 - 00:00
Sunday | Sunday: 10:30 - 00:00 | Sunday: 10:30 - 00:00
(每种方法相同,但是结果会根据所用的开始截止日期而有所不同)
使用您当前的营业时间结构,当营业日和营业日的营业时间均为
AM
时,上述示例将提供误报。 避免这种情况的唯一方法是将星期几包含在给定日期的开闭时间中。
(最准确-如果操作时间不超过24小时)
要解决前一天在AM
开始和结束时误报的问题。问题是关闭的一天是未知的,并且假设运行时间始终少于24小时。您还应该将前一个日期的开放时间与结束日期进行比较,并确保它们不超过24小时。
public static function Today() {
$currentDate = new \DateTimeImmutable;
$priorDate = $currentDate->sub(new \DateTimeInterval('P1D'));
$yesterday = $priorDate->format('l');
$priorOpening = self::setting('opening_time_' . $yesterday);
$priorClosing = self::setting('closing_time_' . $yesterday);
$priorOpenDate = $priorDate->setTime(...explode(':', $priorOpening));
$closeDate = $currentDate->setTime(...explode(':', $priorClosing));
$diff = $priorOpenDate->diff($closeDate);
if ($diff->d === 0 && $currentDate < $closeDate) {
//check if the closing has not occurred yet
$currentDate = $priorDate;
}
$today = $currentDate->format('l');
$start = self::setting('opening_time_' . $today);
$end = self::setting('closing_time_' . $today);
return $start . " - " . $end;
}
或者,由于Mon-Thur
仅在午夜之后开放,因此您可以对要检查的日期进行硬编码,并根据当前时间确定要选择的日期。
此示例仅检查当前日期是否为Tues-Fri
,以及当前时间是否尚未达到01:30
,然后将today
更改为yesterday
。
public static function Today() {
$currentDate = new \DateTimeImmutable;
if (in_array($currentDate->format('N'), [2, 3, 4, 5])) {
//check current time is not beyond the threshold
$closedDate = $currentDate->setTime(1, 30, 00);
if ($currentDate < $closedDate) {
//change the date to display to day prior
$currentDate = $currentDate->sub(new \DateInterval('P1D'));
}
}
$today = $currentDate->format('l');
$start = self::setting('opening_time_' . $today); // Select value from settings table
$end = self::setting('closing_time_' . $today); // Select value from settings table
return $start . " - " . $end;
}