Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would suggest you reconsider the way you deal with the duration of reservations. Rather than having a <code>date_reserved</code> simply have <code>time_in</code>, and <code>time_out</code> as <code>datetime</code>s. I would also use the 24hour format instead of 12hr (with AM &amp; PM). So you would have:</p> <pre><code>$timeInStr = '2012-10-14 10:00:00'; $timeOutStr = '2012-10-14 11:00:00'; </code></pre> <p>Just for completeness, here is some code to insert data into the table:</p> <pre><code>$pdo = new PDO( "mysql:dbname=stack_overflow;host=127.0.0.1", $username, $password); // prepare the insert statement $statement = $pdo-&gt;prepare(" INSERT INTO `reserve`(`time_in`, `time_out`) VALUES (:time_in, :time_out)"); // bind param values $statement-&gt;bindParam(':time_in', $timeInStr, PDO::PARAM_STR); $statement-&gt;bindParam(':time_out', $timeOutStr, PDO::PARAM_STR); $statement-&gt;execute(); </code></pre> <p>In order to determine whether a reservation request would conflict with a previously set reservation, query the database whether the reservation request (equivalent to your <code>$reserve</code> variable) is between the <code>time_in</code> &amp; <code>time_out</code> of any reservation.</p> <p>In this case, I would count how many previous reservations would overlap with the requested reservation time.</p> <pre><code>$statement = $pdo-&gt;prepare(" SELECT COUNT(`id`) FROM `reserve` WHERE :reserve_request_time BETWEEN `time_in` AND `time_out`"); $statement-&gt;bindParam( ':reserve_request_time', '2012-10-14 10:30:00', PDO::PARAM_STR); $statement-&gt;execute(); $result = $statement-&gt;fetch(); // if an overlapping reservation was found "sorry, not available" if (0 &lt; (int) $result['COUNT(`id`)']) { echo "sorry, not available"; } else { echo "ok!"; } </code></pre>
 

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