L
L
lemonlimelike2021-01-27 00:08:13
PHP
lemonlimelike, 2021-01-27 00:08:13

How to find free intervals?

Hello! There is a task to find free intervals of time.

Wrote this method:

private function getFreeInterval($intervals, $startTime, $endTime)
    {
        foreach($intervals as $key => $value){
            $value = explode('-', $value);
            
            $intervalOne = new \DateTime($value[0]);
            $intervalTwo = new \DateTime($value[1]);

            if ($intervalOne >= $startTimeItem && $endTimeItem <= $intervalTwo){
                $intervals[] = "{$intervalOne->format('H:i:s')}-{$startTimeItem->format('H:i:s')}";
                $intervals[] = "{$endTimeItem->format('H:i:s')}-{$intervalTwo->format('H:i:s')}";
                unset($intervals[$key]);
                // dd($endTimeItem, $intervalTwo, $intervalOne, $startTimeItem);
                break;
            }
        }

        return $intervals;
    }


The method performs the following function:
it receives a list of intervals as input, such as:
array:2 [
  0 => "09:30:00-13:15:00"
  1 => "14:00:00-17:00:00"
]

This is a list of free intervals, that is, between 13:15:00 and 14:00:00 is already occupied, the rest is as if free

. Also, the startTime and endTime variables are passed to the method, in which the end and beginning of the client's recording.

And it is necessary that the method constantly returns the correct list. At this point, the method returns the correct result only once. That is, if you run it, then some list of intervals consisting of three elements will return. Then, if these three elements are again passed to the method and some start and end times are set, then the methods will already return an incorrect result.

Here's what I get when I run it for the first time, if I pass this array
array:2 [
  0 => "09:30:00-13:15:00"
  1 => "14:00:00-17:00:00"
]

then it starts like this: 09:30:00 and ends at 10:15:00, then this is what it returns:

array:3 [
1 => "14:00:00-17:00:00"
2 => "09:30: 00-09:30:00"
3 => "10:15:00-13:15:00"
]


The fact that the second element of the array is also wrong, probably.

And further .. if you already pass this array that just arrived, and start = 10:30:00 and end = 11:15:00
Then here is the result right now:
array:4 [
  1 => "09:30:00-09:30:00"
  2 => "10:15:00-13:15:00"
  3 => "14:00:00-10:30:00"
  4 => "11:15:00-17:00:00"
]


Here, it’s not clear where 10:15:00-13:15:00 comes from, if they shouldn’t be
there, there should be an interval of 10:15:00-10:30:00, 11:15:00-13:15:00 and 14:00:00-17:00:00

How to solve this problem? Maybe I don't have that implementation at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-01-27
@lemonlimelike

function getFreeInterval($intervals, $startTime, $endTime)
{
    $result = [];
    $ok = false;
    foreach($intervals as $key => $interval){
        list($start, $end) = explode('-', $interval);
        if ($startTime >= $start && $endTime <= $end) {
            $ok = true;
            if ($startTime > $start) {
                $result[] = "{$start}-{$startTime}";
            }
            if ($endTime < $end) {
                $result[] = "{$endTime}-{$end}";
            }
        } else {
            $result[] = $interval;
        }
    }
    return [
        $ok,
        $result
    ];
}

$intervals = [
  '09:30:00-13:15:00',
  '14:00:00-17:00:00'
];

list($ok, $result) = getFreeInterval($intervals, '09:30:00', '10:15:00');

var_dump($ok, $result);

// bool(true)
// array(2) {
//   [0]=> string(17) "10:15:00-13:15:00"
//   [1]=> string(17) "14:00:00-17:00:00"
// }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question