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
CodePudding user response:
You have to test if you $appointment_start
is is your block time and the same for $appointmnent_end
You can do something like this
$start_time = strtotime($block_time->time_from);
$end_time = strtotime($block_time->time_to);
if ($timeSlot_From >= $start_time && $$timeSlot_From <= $end_time &&
$timeSlot_To >= $start_time && $$timeSlot_To <= $end_time) {
$staff_obj['between_block_time'] = TRUE;
}
CodePudding user response:
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;
}
So your driver code would be like:
<?php
if(isNoCollision(..)){
// do something
}else{
// do something else
}
CodePudding user response:
you can build 2 arrays containing 60 second slots and check, if the they have an intersection, something like that: ! some checks need to be done, eg. times need to end with 00 seconds
function booking_available($start1, $end1, $start2, $end2)
{
$start1 = new DateTime($start1);
$end1 = new DateTime($end1);
$interval1 = new DateInterval('PT60S');
$period1 = new DatePeriod($start1, $interval1, $end1);
$laDate1 = array();
foreach ($period1 as $date) {
$laDate1[] = $date->format('Y-m-d H:i:s');
}
$start2 = new DateTime($start2);
$end2 = new DateTime($end2);
$interval2 = new DateInterval('PT60S');
$period2 = new DatePeriod($start2, $interval2, $end2);
$laDate2 = array();
foreach ($period2 as $date) {
$laDate2[] = $date->format('Y-m-d H:i:s');
}
$laOverlap = array_intersect($laDate1, $laDate2);
$lsEcho = 1; //true
if(count($laOverlap) > 0) {
$lsEcho = 0; //false
}
return $lsEcho;
}
echo booking_available('2022-01-01 13:00:00', '2022-01-01 15:00:00', '2022-01-01 14:00:00', '2022-01-01 15:00:00');
echo booking_available('2022-01-01 13:00:00', '2022-01-01 16:00:00', '2022-01-01 14:00:00', '2022-01-01 15:00:00');
echo booking_available('2022-01-01 13:00:00', '2022-01-01 16:00:00', '2022-01-01 16:00:00', '2022-01-01 17:00:00');