wordpress – OPEN / CLOSED message in PHP based on Store Hours-ThrowExceptions

Exception or error:

I have a company that delivers during certain hours and have some code that seems to be working so far.

The goal is to display a message based on time and delivery status. The only thing I’d like to add is a message 30 minutes before closing time that says “Finishing up current orders. We re-open tomorrow at (open time)”.

How can I add this cleanly?

//--------------------------------------------------
// 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.";
    }
}
How to solve:

If isOpen returns the number of seconds to closing then you can just add an elseif clause to your if that displays the status message; using the opening time from the next open day

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

Note: I’ve used $now in the calls to strtotime but that is not strictly necessary when $now = time(). I put it in to allow for testing e.g. by setting $now = strtotime('2020-03-20 20:45:00');

Leave a Reply

Your email address will not be published. Required fields are marked *