gpt4 book ai didi

php - 基于商店营业时间的 PHP 中的 OPEN/CLOSED 消息

转载 作者:行者123 更新时间:2023-12-04 10:23:58 25 4
gpt4 key购买 nike

我有一家公司在特定时间交付,并且有一些到目前为止似乎可以工作的代码。

目标是根据时间和交付状态显示消息。我唯一想添加的是关闭时间前 30 分钟的一条消息,上面写着“完成当前订单。我们明天在(开放时间)重新开放”。

我怎样才能干净地添加这个?

//--------------------------------------------------
// ADD OPEN OR CLOSED MESSAGE BASED ON TIME

function open_closed_message() {

date_default_timezone_set('America/Toronto');
$date = new DateTime;
echo date("D m/d/y h:i:s",time())

$times = array(
'mon' => '9:00 AM - 9:00 PM',
'tue' => '9:00 AM - 9:00 PM',
'wed' => '9:00 AM - 9:00 PM',
'thu' => '9:00 AM - 9:00 PM',
'fri' => '9:00 AM - 9:00 PM',
'sat' => '11:00 AM - 6:00 PM',
'sun' => 'closed'
);

function compileHours($times, $timestamp) {
$times = $times[strtolower(date('D',$timestamp))];
if(!strpos($times, '-')) return array();
$hours = explode(",", $times);
$hours = array_map('explode', array_pad(array(),count($hours),'-'), $hours);
$hours = array_map('array_map', array_pad(array(),count($hours),'strtotime'), $hours, array_pad(array(),count($hours),array_pad(array(),2,$timestamp)));
end($hours);
if ($hours[key($hours)][0] > $hours[key($hours)][1]) $hours[key($hours)][1] = strtotime('+1 day', $hours[key($hours)][1]);
return $hours;
}

function isOpen($now, $times) {
$open = 0; // time until closing in seconds or 0 if closed
// merge opening hours of today and the day before
$hours = array_merge(compileHours($times, strtotime('yesterday',$now)),compileHours($times, $now));

foreach ($hours as $h) {
if ($now >= $h[0] and $now < $h[1]) {
$open = $h[1] - $now;
return $open;
}
}
return $open;
}

$now = time();
$open = isOpen($now, $times);

if ($open == 0) {
echo "Closed - Sorry, we are not making deliveries.";
} else {
echo "Open - Yes, we are making deliveries.";
}
}

最佳答案

如果 isOpen返回关闭的秒数,然后您可以添加 elseif您的 if 的条款显示状态消息;使用下一个开放日的开放时间

if ($open == 0) {
echo "Closed - Sorry, we are not making deliveries.";
}
elseif ($open <= 1800) {
$tomorrow = strtotime('tomorrow', $now);
if (date('N', $tomorrow) == 7) {
$tomorrow = strtotime('next monday', $now);
}
$day = strtolower(date('D', $tomorrow));
$tomorrow = date('l', $tomorrow);
$opentime = preg_replace('/^(\d+:\d+ [AP]M).*/', '$1', $times[$day]);
echo "Finishing up current orders. We re-open $tomorrow at $opentime";
}
else {
echo "Open - Yes, we are making deliveries.";
}

Demo on 3v4l.org

注意:我用过 $now在对 strtotime 的调用中但在 $now = time() 时,这并不是绝对必要的。 .我把它放进去允许测试,例如通过设置 $now = strtotime('2020-03-20 20:45:00');

关于php - 基于商店营业时间的 PHP 中的 OPEN/CLOSED 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60713869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com