php - how to include time slot breaks using DateTime

3k Views Asked by At

I want to create time slots with start, end time & also break start and end.
I have achieved to generate the time slots but without the break between 11:10 - 11:25

LIVE CODE EXAMPLE

Input variables

$duration = 35; // how much the is the duration of a time slot
$cleanup    = 0; // don't mind this
$start    = '09:00'; // start time
$end      = '12:00'; // end time
$break_start = '11:10'; // break start
$break_end   = '11:25'; // break end

function availableSlots($duration, $cleanup, $start, $end) {
    $start         = new DateTime($start);
    $end           = new DateTime($end);
    $interval      = new DateInterval("PT" . $duration . "M");
    $cleanupInterval = new DateInterval("PT" . $cleanup . "M");
    $periods = array();

    for ($intStart = $start; $intStart < $end; $intStart->add($interval)->add($cleanupInterval)) {
        $endPeriod = clone $intStart;
        $endPeriod->add($interval);

        if ($endPeriod > $end) {
            break;
        }

        $periods[] = $intStart->format('H:i A') . ' - ' . $endPeriod->format('H:i A');
    }


    return $periods;
}

Function will generate this output:

09:00 AM - 09:35 AM
09:35 AM - 10:10 AM
10:10 AM - 10:45 AM
10:45 AM - 11:20 AM
11:20 AM - 11:55 AM

What i need to achieve is 11:10am till 11:25am break period to be excluded from the results.

$break_start = '11:10';
$break_end   = '11:25';
1

There are 1 best solutions below

2
On

Try this... I have modified your function

function availableSlots($duration, $cleanup, $start, $end, $break_start, $break_end) {
    $start         = new DateTime($start);
    $end           = new DateTime($end);
    $break_start           = new DateTime($break_start);
    $break_end           = new DateTime($break_end);
    $interval      = new DateInterval("PT" . $duration . "M");
    $cleanupInterval = new DateInterval("PT" . $cleanup . "M");
    $periods = array();

    for ($intStart = $start; $intStart < $end; $intStart->add($interval)->add($cleanupInterval)) {
        $endPeriod = clone $intStart;
        $endPeriod->add($interval);

        if(strtotime($break_start->format('H:i A')) < strtotime($endPeriod->format('H:i A')) && strtotime($endPeriod->format('H:i A')) < strtotime($break_end->format('H:i A'))){
            $endPeriod = $break_start;
            $periods[] = $intStart->format('H:i A') . ' - ' . $endPeriod->format('H:i A');
            $intStart = $break_end;
            $endPeriod = $break_end;
            $intStart->sub($interval);
        }else{
            $periods[] = $intStart->format('H:i A') . ' - ' . $endPeriod->format('H:i A');
        }
    }


    return $periods;
}

$duration = 35; // how much the is the duration of a time slot
$cleanup    = 0; // don't mind this
$start    = '09:00'; // start time
$end      = '12:00'; // end time
$break_start = '11:10'; // break start
$break_end   = '11:25'; // break end
availableSlots($duration, $cleanup, $start, $end, $break_start, $break_end);

The Result will be:

Array
(
    [0] => 09:00 AM - 09:35 AM
    [1] => 09:35 AM - 10:10 AM
    [2] => 10:10 AM - 10:45 AM
    [3] => 10:45 AM - 11:10 AM
    [4] => 11:25 AM - 12:00 PM
)