anonymous
anonymous

Reputation: 1

Search between two times (e.g. 14:00:00 - 16:00:00)

can anyone here tell me how to write code to search for a time. For example in my database, I have start_time and end_time like this:

start_time   end_time
14:00:00     16:00:00

I have php code to search for it if I put in the text field to find for the time 14:00:00 to 16:00:00, but

My question is how can I get the same result if user search for the time between it ?

For example, if user search for the time 15:00:00 to 16:00:00.

sorry for my english..hope anybody can help me..thank you.

below is my code.

FORM

      <input name="intime" type="time" placeholder='Insert Time'>
      <input name="intime2" type="time" placeholder='Insert Time'>
      <input name="submit" type="submit" value="SEARCH" />

      </form>

Upvotes: 0

Views: 74

Answers (2)

James Lalor
James Lalor

Reputation: 1246

Since you're looking for times between two times, we can use the AND clause in our Query, utilising more than (and equal to) with less than (and equal to) - This returns results equal to the two form inputs (intime and intime2) as well as the results inbetween them.

WHERE time >= $intime AND time <= $intime2

As for what you're asking, have you tried...

$intime = (isset($_POST['intime']) ? $_POST['intime'] : 0);
$intime2 = (isset($_POST['intime2']) ? $_POST['intime2'] : 0);

$query = "SELECT start_time, end_time FROM reservation WHERE" . 
    ($intime ? " start_time >= '%$intime%'" : "") . 
    ($intime && $intime2 ? " AND" : "") .
    ($intime2 ? " end_time <= '%$intime2%'" : "");

Upvotes: 1

xjliao
xjliao

Reputation: 23

where time between start_time and end_time;

Upvotes: 1

Related Questions