【问题标题】:OPEN / CLOSED message in PHP based on Store Hours基于商店营业时间的 PHP 中的 OPEN / CLOSED 消息
【发布时间】: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


    【解决方案1】:

    如果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

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

    【讨论】:

    • 这很好用!太感谢了。但是有一个问题 - “完成当前订单”是否正确归因于周六他们提前关闭(下午 6 点)以及他们整个周日都关闭的事实?目前无法发短信。
    • @RonnieMcDonte 是的 - 您的代码处理关闭时间,这处理确定下一个开放日是星期一(这就是 if (date('N', $tomorrow) == 7) 检查的内容)。看看3v4l.org/rKpQI,我将$now 硬连线到下周六5:35。您可以将其用作其他日期和时间的测试平台。
    猜你喜欢
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 2010-10-20
    • 2011-11-09
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多