Question

All i want, to calculate time-slots-list according to my business-hours and block or disable those time-slots which booked by appointments.

I have done these things with my problem, stuck on last task, mention below.

My Service Duration = 60min.
My Today Busines Hours:
start time - 10:00 AM
end time - 3:00 PM

I have generated 15min time slot-list according to my today business hours:

$start = strtotime('10:00 AM');
$end = strtotime('3:00 PM');
for( $i = $start; $i <= $end; $i += (60*15)) 
{
    $slotlist[] = date('g:iA', $i);
}

//output
10:00AM
10:15AM
10:30AM
10:45AM
11:00AM
11:15AM
11:30AM
11:45AM
12:00PM
12:15PM
12:30PM
12:45PM
1:00PM
1:15PM
1:30PM
1:45PM
2:00PM
2:15PM
2:30PM
2:45PM
3:00PM

now i have booked 3 appointments

Appointments | start-time to end-times


Appointment-1 : 10:00AM to 11:00AM (according to my service duration 60min)
Appointment-2 : 11:15AM to 12:15PM
Appointment-3 : 1:00PM to 2:00PM


//array of all appointment start_time
$appointment_start_time = array('10:00AM','11:15AM','1:00PM');

Now disable those time-slots which are already booked in appointments

foreach($slotlist as $single)
{
    if(in_array($single, $appointment_start_time))
    {
        echo "<input name=start_time type='radio' disabled  value='$single' />".$single; echo "<br>";

        $time = $single;                                                
        $event_length = 60-15;          // 60 is time duration                              
        $timestamp = strtotime("$time");                                
        $endtime = strtotime("+$event_length minutes", $timestamp);     
        $next_time = date('g:i A', $endtime);                       

        //calculate between slots (start-end time)
        $start = strtotime($single);
        $end = strtotime($next_time);
            for( $i = $start; $i <= $end; $i += (60*15)) 
            {
                $list2[] = date('g:iA', $i);
            }
    }
    else
    {   
        if(in_array($single, $list2))
        {
            echo "<input name=start_time type='radio' disabled  value='$single' />".$single; echo "<br>";
        }
        else
        {   

            echo "<input name=start_time type='radio'  value='$single' />".$single; echo "<br>";
        }
    }

}

//output
10:00AM -disabled
10:15AM -disabled
10:30AM -disabled
10:45AM -disabled
11:00AM
11:15AM -disabled
11:30AM -disabled
11:45AM -disabled
12:00PM -disabled
12:15PM
12:30PM
12:45PM
1:00PM -disabled
1:15PM -disabled
1:30PM -disabled
1:45PM -disabled
2:00PM
2:15PM
2:30PM
2:45PM
3:00PM

Now last task to do, how to remove those time slot which are not qualify service duration time like as:

11:00AM (single slot b/w 2 appointment, cover only 15min)

12:15PM -
12:30PM -(3 slot b/w 2 appointment, cover only 45min)
12:45PM -
Was it helpful?

Solution

2 days gone, i made this thing to be possible

$slottimes = 15;
$duration = 60; 
$business_start = '10:00AM';
$business_end = '3:00PM';
echo "<b>Today's Wroking Hours $business_start to $business_end</b><br>";
echo "<b>Calendar Slot(s) Time:</b> $slottimes<br>";
echo "<b>Service Duration Time:</b> $duration<br><br>";
$start = strtotime($business_start);
$end = strtotime($business_end);
/*$endtimestamp = strtotime($business_end);
$end = strtotime("-$slottimes minutes", $endtimestamp);*/
    for( $i = $start; $i <= $end; $i += (60*$slottimes)) 
    {
        $slotlist[] = date('g:iA', $i);
    }

echo "<b>Generate Today's Available Appointment Time(s)</b><br>";
echo "<pre>"; print_r($slotlist); echo "</pre>";

echo "<b>Today's Booked Appointment(s)</b>";
$appstarttime = array('11:00AM', '11:15AM', '1:00PM', $business_end );          //business end-time always be in array
$previoustime = NULL;
$nexttime = array($business_end);

foreach($slotlist as $single)
{
    if(in_array($single, $appstarttime))
    {
        //calculate next time
        $time = $single;                                                
        $event_length = $duration-$slottimes;           // 60min Service duration time  - 15 slot time                          
        $timestamp = strtotime("$time");                                
        $endtime = strtotime("+$event_length minutes", $timestamp);     
        $next_time = date('g:i A', $endtime);                       

        //calculate between slots (start-end time)
        $start = strtotime($single);
        $end = strtotime($next_time);
            for( $i = $start; $i <= $end; $i += (60*$slottimes)) 
            {
                $nexttime[] = date('g:iA', $i);
            }



        //calculate previous time
        $time1 = $single;                                               
        $event_length1 = $duration-$slottimes;          // 60min Service duration time - 15 slot time                               
        $timestamp1 = strtotime("$time1");                              
        $endtime1 = strtotime("-$event_length1 minutes", $timestamp1);  
        $next_time1 = date('g:i A', $endtime1);                         

        //calculate between slots (start-end time)
        $start1 = strtotime($next_time1);
        $end1 = strtotime($single);
            for( $i = $start1; $i <= $end1; $i += (60*$slottimes)) 
            {
                $previoustime[] = date('g:iA', $i);
            }


    }
}
echo "<br>Appointment Start Time: ";
print_r($appstarttime);
echo "<br>";

echo "<br><strong>Previous Time: </strong>";
print_r($previoustime);
echo "<br>";

echo "<br><strong>Next Time: </strong>";
print_r($nexttime);
echo "<br>";

echo "<br><strong>Merge Both Time:</strong> ";
if($previoustime && $nexttime)
{
    $merge = array_merge($previoustime, $nexttime);
    print_r($merge);
}
echo "<br>";echo "<br>";

$flag1 = 0;
$flag2 = 0;
echo "<hr><b>Today's Available Time For Appointment:</b><hr>";
foreach($slotlist as $single)
{
    if(in_array($single, $merge))
    {
        echo "<input name=start_time type='radio' disabled  value='$single' />".$single; echo "<br>";
    }
    else
    {
        echo "<input name=start_time type='radio'  value='$single' />".$single; echo "<br>";
    }

    if(in_array($single, $merge))
    {
        $flag1 = 1;
    }
    else
    {
        $flag2 = 2;
    }

}

if($flag2 != 2)
{   echo "Sorry ! Today's all appointments has been booked."; }

echo "<hr>";

OTHER TIPS

Try this: 1.compare the first time-slot with $appointment_start_time[0],if the first time-slot > $appointment_start_time[0]-60min,disable the time-slot till $appointment_start_time[0].

2.compare $appointment_start_time[1] with $appointment_start_time[0],if duration time > 2 hour(your service duration time X 2),disable the time-slot between $appointment_start_time[0] and $appointment_start_time[1]

3.compare $appointment_start_time[2] with end-time,if end-time-120 < $appointment_start_time[2],also disabled

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top