【发布时间】:2020-03-16 22:12:14
【问题描述】:
我有一家公司在特定时间提供服务,并且有一些代码目前看来可以正常工作。
目标是根据时间和交付状态显示消息。我唯一想补充的是在关闭时间前 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.";
}
}
【问题讨论】:
标签: php wordpress date woocommerce