Note that there are some explanatory texts on larger screens.

plurals
  1. POIs the current time between two given times?
    primarykey
    data
    text
    <p>I'm trying to calculate <strong>if the current time is within the opening hours of a restaurant</strong>.</p> <p>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.</p> <p>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.</p> <p>Anyway, here is what I have. Please tell me a better way to do it.</p> <p>I have times stored like this:</p> <pre class="lang-php prettyprint-override"><code>$times = array( 'opening_hours_mon' =&gt; '9am - 8pm', 'opening_hours_tue' =&gt; '9am - 2am', 'opening_hours_wed' =&gt; '8:30am - 2am', 'opening_hours_thu' =&gt; '5:30pm - 2am', 'opening_hours_fri' =&gt; '8:30am - 11am', 'opening_hours_sat' =&gt; '9am - 3pm, 5pm - 2am', 'opening_hours_sun' =&gt; 'closed' ); </code></pre> <p>This is the code I'm using now:</p> <pre class="lang-php prettyprint-override"><code>// 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 &lt;= $now) &amp;&amp; ($now &lt;= $close_time)) { $status = 'open'; break; } else { $status = 'closed'; } } } </code></pre> <p>NOTE: <a href="http://codex.wordpress.org/Function_Reference/current_time" rel="nofollow noreferrer">current_time('timestamp',0) is a WordPress function</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload