PHP Display business hours of operation
<p>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.</p>

<pre><code>$tuesdayopen = strtotime("10:00");
$tuesdayclose = strtotime("23:00");
$now = strtotime("now");

if ($now &gt;= $tuesdayopen &amp;&amp; $now &lt;= $tuesdayclose) {
    echo "We are open!";
} else {
    echo "We are closed!";
}
</code></pre>

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

<pre><code>$tuesdayopen = strtotime("10:00");
$tuesdayclose = strtotime("02:00");
$now = strtotime("now");

if ($tuesdayclose &gt;= strtotime("00:00") &amp;&amp; $tuesdayclose &lt;= strtotime("04:00")):
    $tuesdayclose = strtotime("+1 day", $tuesdayclose);
endif;

if ($now &gt;= $tuesdayopen &amp;&amp; $now &lt;= $tuesdayclose) {
    echo "We are open!";
} else {
    echo "We are closed!";
}
</code></pre>

<p>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.</p>
