Check if business is open using current time and an array with days as keys and hyphen-separated time ranges as values

current-time, php, time, timestamp

Solution

Here is my object-oriented solution, based on the usage of the PHP DateTime class (available since the 5.2 version):

<?php 

class Restaurant {
    private $cw;
    private $times = array();
    private $openings = array();

    public function __construct(array $times) {
        $this->times = $times;
        $this->setTimes(date("w") ? "this" : "last");
        //print_r($this->openings);       // Debug
    }

    public function setTimes($cw) {
        $this->cw = $cw;
        foreach ($this->times as $key => $val) {
            $t = array();
            $buf = strtok($val, ' -,');
            for ($n = 0; $buf !== FALSE; $n++) {
                try {
                    $d = new DateTime($buf);
                    $d->setTimestamp(strtotime(substr($key, -3)." {$this->cw} week {$buf}"));
                    if ($n && ($d < $t[$n-1])) {
                        $d->add(new DateInterval('P1D'));
                    }
                    $t[] = $d;
                } catch (Exception $e) {
                    break;
                }
                $buf = strtok(' -,');
            }
            if ($n % 2) {
                throw new Exception("Invalid opening time: {$val}");
            } else {
                $this->openings[substr($key, -3)] = $t;
            }
        }
    }

    public function isOpen() {
        $cw = date("w") ? "this" : "last";
        if ($cw != $this->cw) {
            $this->setTimes($cw);
        }
        $d = new DateTime('now');
        foreach ($this->openings as $wd => $t) {
            $n = count($t);
            for ($i = 0; $i < $n; $i += 2) {
                if (($d >= $t[$i]) && ($d <= $t[$i+1])) {
                    return(TRUE);
                }
            }
        }
        return(FALSE);
    }
}

$times = array(
    'opening_hours_mon' => '9am - 8pm',
    'opening_hours_tue' => '9am - 2am',
    'opening_hours_wed' => '8:30am - 2am',
    'opening_hours_thu' => '9am - 3pm',
    'opening_hours_fri' => '8:30am - 11am',
    'opening_hours_sat' => '9am - 3pm, 5pm - 2am',
    'opening_hours_sun' => 'closed'
);

try {
    $r = new Restaurant($times);
    $status = $r->isOpen() ? 'open' : 'closed';
    echo "status=".$status.PHP_EOL;
} catch (Exception $e) {
    echo $e->getMessage().PHP_EOL;
}

?>

As you can see, the constructor builds an internal form (the `openings` array of DateTime objects), which is then used with a simple comparison in the `isOpen` method to check if at the time of the call the restaurant is opened or closed.

You'll also notice that I've used the DateTime:add method to calculate tomorrow's date, instead of adding 86400 (24*60*60) to the current date timestamp, to avoid problems with DST time shifts. Proof of concept:

<?php

ini_set("date.timezone", "Europe/Rome");
echo "date.timezone = ".ini_get("date.timezone").PHP_EOL;

$d1 = strtotime("2013-10-27 00:00:00");
$d2 = strtotime("2013-10-28 00:00:00");
// Expected: 86400, Result: 90000
echo "Test #1: ".($d2 - $d1).PHP_EOL;
// Expected: 2013-10-28 00:00:00, Result: 2013-10-27 23:00:00
echo "Test #2: ".date("Y-m-d H:i:s", $d1 + 86400).PHP_EOL;

$d1 = strtotime("2014-03-30 00:00:00");
$d2 = strtotime("2014-03-31 00:00:00");
// Expected: 86400, Result: 82800
echo "Test #3: ".($d2 - $d1).PHP_EOL;
// Expected: 2014-03-30 00:00:00, Result: 2014-03-29 23:00:00
echo "Test #4: ".date("Y-m-d H:i:s", $d2 - 86400).PHP_EOL;

?>

Which gives the following results:

date.timezone = Europe/Rome
Test #1: 90000
Test #2: 2013-10-27 23:00:00
Test #3: 82800
Test #4: 2014-03-29 23:00:00

Therefore, it seems that one day not always has 86400 seconds; at least not twice a year...

Problem

I'm trying to calculate if the current time is within the opening hours of a restaurant. This question has been asked a lot on Stackoverflow, but I haven't found one that can account for the problems I am having. Also, would be nice to see idea on a better way to do this. Currently it breaks if the day is closed (Sunday in this example) or if it's 1am on "Saturday" (so technically 1am Sunday morning). I have a feeling I'll have to change the way the data is stored to account for after midnight, but I'm trying to work with what I have for now. It's a problem, because most restaurants list their opening times for a given day as 5pm - 2am, not 5pm - 12am, 12am - 2am. Anyway, here is what I have. Please tell me a better way to do it. I have times stored like this: ``` $times = array( 'opening_hours_mon' => '9am - 8pm', 'opening_hours_tue' => '9am - 2am', 'opening_hours_wed' => '8:30am - 2am', 'opening_hours_thu' => '5:30pm - 2am', 'opening_hours_fri' => '8:30am - 11am', 'opening_hours_sat' => '9am - 3pm, 5pm - 2am', 'opening_hours_sun' => 'closed' ); ``` This is the code I'm using now: ``` // Get the right key for today $status = 'open'; $now = (int) current_time( 'timestamp' ); $day = strtolower( date('D', $now) ); $string = 'opening_hours_'.$day; $times = $meta[$string][0]; // This should be a stirng like '6:00am - 2:00am' or even '6:00am - 11:00am, 1:00pm to 11:00pm'. // Does it contain a '-', if not assume it's closed. $pos = strpos($times, '-'); if ($pos === false) { $status = 'closed'; } else { // Maybe a day has multiple opening times? $seating_times = explode(',', $times); foreach( $seating_times as $time ) { $chunks = explode('-', $time); $open_time = strtotime($chunks[0]); $close_time = strtotime($chunks[1]); // Calculate if now is between range of open and closed if(($open_time <= $now) && ($now <= $close_time)) { $status = 'open'; break; } else { $status = 'closed'; } } } ``` NOTE: current_time('timestamp',0) is a WordPress function.

Original source