bionemesis
bionemesis

Reputation: 43

PHP Display business hours of operation

I am trying to display a notice when a business is open on a Wordpress-based website and pulling the information from a database. The data is stored simply as opening/closing times for each day, as separate database entries. It is fairly straight forward when the hours are within a single day. But for a bar/restaurant, it becomes complex because the closing time is technically in the next day. I'll use Tuesday as an example.

$tuesdayopen = strtotime("10:00");
$tuesdayclose = strtotime("23:00");
$now = strtotime("now");

if ($now >= $tuesdayopen && $now <= $tuesdayclose) {
    echo "We are open!";
} else {
    echo "We are closed!";
}

So this works fine as is. And if the closing time is into next day, I have an inelegant solution for that too.

$tuesdayopen = strtotime("10:00");
$tuesdayclose = strtotime("02:00");
$now = strtotime("now");

if ($tuesdayclose >= strtotime("00:00") && $tuesdayclose <= strtotime("04:00")):
    $tuesdayclose = strtotime("+1 day", $tuesdayclose);
endif;

if ($now >= $tuesdayopen && $now <= $tuesdayclose) {
    echo "We are open!";
} else {
    echo "We are closed!";
}

So where is my problem? The problem is when $now ends up being after midnight. So if someone visits the page Tuesday night, after midnight, it is technically Wednesday morning. Which means that my full script would move on to Wednesday. I need it to recognize that it is between midnight and 4AM and as such, use the previous days open/closing times to determine if it is open (but only if the closing time is after midnight, otherwise moving on to Wednesday is the right thing to do). Hopefully I explained that well. I have tried writing something myself, but the code is a bloated mess and ultimately doesn't work. So I won't bother including it here.

Upvotes: 4

Views: 2638

Answers (2)

Arnold Daniels
Arnold Daniels

Reputation: 16553

I would create an array with times that the place is open and loop through that. Additional advantage is that if a place closes during lunch for instance, it's easy to add.

Note that I normalize the time to count seconds from monday 00:00, regardless it the time is in the future or past.

<?php

// Normalize time to count from 0
function timeOfWeek($time) {
  if (!is_int($time)) $time = strtotime($time) ;
  $secondsInWeek = (7 * 24 * 3600);
  return (($time - strtotime('monday 00:00')) % $secondsInWeek + $secondsInWeek) % $secondsInWeek;
}

function isOpen($opened, $time) {
  $time = timeOfWeek($time);

  foreach ($opened as $openday) {
    list($open, $close) = $openday;

    if ($open < $close) { 
      if ($time > $open && $time < $close) return true;
    } else {
      if ($time > $open || $time < $close) return true; // Special case sunday -> monday
    }
  }

  return false;
}

$opened = array();
$opened[] = array(timeOfWeek('monday 10:00'), timeOfWeek('monday 23:00'));
$opened[] = array(timeOfWeek('tuesday 10:00'), timeOfWeek('tuesday 23:00'));
$opened[] = array(timeOfWeek('wednesday 10:00'), timeOfWeek('wednesday 23:00'));
$opened[] = array(timeOfWeek('thursday 10:00'), timeOfWeek('thursday 23:00'));
$opened[] = array(timeOfWeek('friday 10:00'), timeOfWeek('saturday 01:00'));
$opened[] = array(timeOfWeek('saturday 10:00'), timeOfWeek('sunday 01:00'));
$opened[] = array(timeOfWeek('sunday 10:00'), timeOfWeek('monday 01:00'));

$open = isOpen($opened, time());
var_dump($open);

Upvotes: 3

mrash
mrash

Reputation: 903

simply you can convert timezone to yourTimeZone-4h!

Upvotes: -2

Related Questions