Skip to content
Advertisement

Check TimeSlot is between another Time Slot

So I have block time and appointment time let’s say block time is

$block_start = "13:00"
$block_end = "15:00"

and the appointment time is

$appointment_start = "14:00"
$appointment_end = "15:00"

now I want to check if the appointment time is between block time and vice versa

my current code

   $start_time = strtotime($block_time->time_from);
   $end_time = strtotime($block_time->time_to);
   if ($timeSlot_From >= $start_time && $timeSlot_To <= $end_time) {
       $staff_obj['between_block_time'] = TRUE;
   }

other conditions

condition 1

if appointment time is

 $appointment_start = 13:00
 $appointment_end = 16:00

and block time is

$block_start = 14:00
$block_end = 15:00

then also it should not allow booking

condition 2

if block time is

$block_start = 13:00
$block_end = 16:00

and appointment time is

$appointment_start = 14:00
$appointment_end = 15:00

condition 3

if appointment time is 13:00-14:00 and block time is 14:00-15:00 then appointment booking is allowed

Advertisement

Answer

So 2 times won’t collide if one starts after the other one ends OR one ends before the other one starts.

Snippet:

<?php

function isNoCollision($t1s, $t1e, $t2s, $t2e){
    $t1s = getTimeInSeconds($t1s);
    $t1e = getTimeInSeconds($t1e);
    $t2s = getTimeInSeconds($t2s);
    $t2e = getTimeInSeconds($t2e);
    return $t1e <= $t2s || $t1s >= $t2e;
}

Online Demo

So your driver code would be like:

<?php

if(isNoCollision(..)){
  // do something
}else{
  // do something else
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement