Stack Exchange
Check if time interval does fall inside other time intervals
I want to determine if given interval doesn't fall inside other intervals (using 24-hour clock format), here is the example:
case1:
$startDate = '2021-04-05 08:05:00';
$endDate = '2021-04-05 10:05:00';

$intervals = [['startDate' => '2021-04-05 08:30:00', 'endDate' => '2021-04-05 11:05:00'], ['startDate' => '2021-04-05 12:30:00', 'endDate' => '2021-04-05 13:05:00']]

In this case we should set:
$flagCanSchedule = 0;

Because 2021-04-05 08:05:00 to 2021-04-05 10:05:00 falls between this interval:

'startDate' => '2021-04-05 08:30:00', 'endDate' => '2021-04-05
11:05:00'

But for this:
case 2:
$startDate = '2021-04-05 08:05:00';
$endDate = '2021-04-05 10:05:00';

$intervals = [['startDate' => '2021-04-05 10:05:00', 'endDate' => '2021-04-05 11:05:00'], ['startDate' => '2021-04-05 07:30:00', 'endDate' => '2021-04-05 08:05:00']]

In this case set:
$flagCanSchedule = 1;

Because we don't overlap any other interval, evan tho ('2021-04-05 08:05:00' == '2021-04-05 08:05:00').
I wanted to solve this using strtotime and transform the dates to unix format, but I'm not sure how to write my if statement.
$startDate = strtotime('2021-04-05 08:05:00');
$endDate = strtotime('2021-04-05 10:05:00');
foreach($intervals as $interval) {
   if($startDate <= strtotime($interval['startDate']) && $endDate <= 
   strtotime($interval['endDate'])) {
       $flagCanSchedule = 1;
   } else {
       $flagCanSchedule = 0;
   }
}


You should add a break when a flagCanSchedule is found. Because, the next iterations overrides this value (from 1 to 0).
$intervals = [
    ['startDate' => '2021-04-05 10:05:00', 'endDate' => '2021-04-05 11:05:00'], 
    ['startDate' => '2021-04-05 07:30:00', 'endDate' => '2021-04-05 08:05:00'],
];

$startDate = strtotime('2021-04-05 08:05:00');
$endDate = strtotime('2021-04-05 10:05:00');
foreach($intervals as $interval)
{
   if ($startDate <= strtotime($interval['startDate']) && 
       $endDate <= strtotime($interval['endDate'])) 
   {
       $flagCanSchedule = 1;
       break; // << stop the test
   } 
   else {
       $flagCanSchedule = 0;
   }
}
var_dump($flagCanSchedule); // int(1)

You also can remove the else statement by initializing the flag first.
$flagCanSchedule = 0;
foreach ($intervals as $interval) 
{
   if ($startDate <= strtotime($interval['startDate']) && 
       $endDate <= strtotime($interval['endDate'])) 
   {
       $flagCanSchedule = 1;
       break;
   }
}
var_dump($flagCanSchedule); // int(1)


That won't work for the fallowing case ['startDate' => '2021-04-05 08:06:00', 'endDate' => '2021-04-05 11:00:00'], and the $startDate = strtotime('2021-04-05 08:05:00');
$endDate = strtotime('2021-04-05 10:05:00'); it will overlap intervals!

That also won't work, check my edited example here: https://3v4l.org/bnB9R

Note that intervals can be any random interval of dates with time. Basically is a schedule calculator that should output 1 only when there is a free schedule period. My above example (in the link) outputs 1 evan tho 2021-04-05 10:05:00 will fall in the interval.

updated. If I understand, in case 1, flag is 1 because $startDate is not in interval. in case 2, flag is 1, because $startDate en $endDate are not in intervals, case 3, flag is 0 because $startDate en $endDate are not in the interval?

its not only about $startDate, let's take a practical example, you are a worker and you work between '2021-04-05 08:05:00' and '2021-04-05 10:05:00' at a place, and someone else want to schedule you another job between this interval '2021-04-05 08:06:00' and '2021-04-05 11:00:00' that shouldn't be allowed because you obviously let's say at  '2021-04-05 09:00:00' you are still scheduled for the first place on this interval: '2021-04-05 08:05:00' and '2021-04-05 10:05:00'

I want that let's lower the limit: say between 2021-04-05 10:00:00 and 2021-04-05 10:05:00, that means: 2021-04-05 10:01:00, 2021-04-05 10:02:00, 2021-04-05 10:03:00, 2021-04-05 10:04:00 , this 5 minutes, you can't be scheduled.
