Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my object-oriented solution, based on the usage of the PHP <a href="http://www.php.net/manual/en/class.datetime.php" rel="nofollow">DateTime class</a> (available since the 5.2 version):<br></p> <pre><code>&lt;?php class Restaurant { private $cw; private $times = array(); private $openings = array(); public function __construct(array $times) { $this-&gt;times = $times; $this-&gt;setTimes(date("w") ? "this" : "last"); //print_r($this-&gt;openings); // Debug } public function setTimes($cw) { $this-&gt;cw = $cw; foreach ($this-&gt;times as $key =&gt; $val) { $t = array(); $buf = strtok($val, ' -,'); for ($n = 0; $buf !== FALSE; $n++) { try { $d = new DateTime($buf); $d-&gt;setTimestamp(strtotime(substr($key, -3)." {$this-&gt;cw} week {$buf}")); if ($n &amp;&amp; ($d &lt; $t[$n-1])) { $d-&gt;add(new DateInterval('P1D')); } $t[] = $d; } catch (Exception $e) { break; } $buf = strtok(' -,'); } if ($n % 2) { throw new Exception("Invalid opening time: {$val}"); } else { $this-&gt;openings[substr($key, -3)] = $t; } } } public function isOpen() { $cw = date("w") ? "this" : "last"; if ($cw != $this-&gt;cw) { $this-&gt;setTimes($cw); } $d = new DateTime('now'); foreach ($this-&gt;openings as $wd =&gt; $t) { $n = count($t); for ($i = 0; $i &lt; $n; $i += 2) { if (($d &gt;= $t[$i]) &amp;&amp; ($d &lt;= $t[$i+1])) { return(TRUE); } } } return(FALSE); } } $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; '9am - 3pm', 'opening_hours_fri' =&gt; '8:30am - 11am', 'opening_hours_sat' =&gt; '9am - 3pm, 5pm - 2am', 'opening_hours_sun' =&gt; 'closed' ); try { $r = new Restaurant($times); $status = $r-&gt;isOpen() ? 'open' : 'closed'; echo "status=".$status.PHP_EOL; } catch (Exception $e) { echo $e-&gt;getMessage().PHP_EOL; } ?&gt; </code></pre> <p>As you can see, the constructor builds an internal form (the <code>openings</code> array of DateTime objects), which is then used with a simple comparison in the <code>isOpen</code> method to check if at the time of the call the restaurant is opened or closed.<br></p> <p>You'll also notice that I've used the <a href="http://www.php.net/manual/en/datetime.add.php" rel="nofollow">DateTime:add</a> method to calculate tomorrow's date, instead of adding 86400 (24*60*60) to the current date timestamp, to avoid problems with <a href="http://en.wikipedia.org/wiki/Daylight_saving_time" rel="nofollow">DST</a> time shifts.<br> Proof of concept:<br></p> <pre><code>&lt;?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; ?&gt; </code></pre> <p>Which gives the following results:<br></p> <pre><code>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 </code></pre> <p>Therefore, it seems that one day not always has 86400 seconds; at least not twice a year...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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