How to prevent time overlap in booking sytem

hi guys, i am working on a conference room booking system and need to prevent time overlap. i have written this query to make sure the same time is not book by more then two client but is not working please.
                   $statime = $_POST['stime'];
		  $endtime = $_POST['etime'];

$qu = mysqli_query($mysqli, "SELECT COUNT(*) FROM mohconferenceroom WHERE starttime >= '$statime' AND endtime <= '$endtime'");
      if(mysqli_num_rows($qu) >= 1){	
        $bookmessage = " * this time is already book.";
		$albookvalid = 0;
        $successvalid = 1;

		  $query =mysqli_query($mysqli,"insert into mohconferenceroom(department,unit,startdate,enddate,starttime,endtime,purpose,itemneeded) values ('$dep','$uni','$stdate','$endate',' $statime','$endtime','$purpose','$chk')");	
          $success = "form submitted successifully";
    }     $successvalid = 0;

Open in new window

Moses DwanaAsked:
Who is Participating?
Scott Fell, EE MVEConnect With a Mentor Developer & EE ModeratorCommented:
I have  used this one myself

WHERE (startTime >= @StartDate and startTime < @EndDate)
OR    (endTime <= @EndDate and EndTime > @StartDate)
OR   ( StartTime <= @StartDate and Endtime >= @EndDate) 

Open in new window

Note, your table names are different
WHERE (starttime >= $starttime  and starttime < $endtime )
OR    (endTime <= $endtime and endtime > $starttime)
OR   ( starttime <= $starttime and endtime >= $endtime ) 

Open in new window

(no points .. this is from the same link above)

There's also another method on that thread:

WHERE @EndDate > StartTime AND @StartDate <= EndTime

I've used both methods and either works fine.
The proposed query detects overlapping reservations.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.