U
U
undefo2018-02-23 14:57:04
PHP
undefo, 2018-02-23 14:57:04

How to check if the work is included in the time period?

Hey!
There is a working day 9:00-21:00 .
There are busy intervals with a multiplicity of 15 minutes. For example, 10:00-10:15, 10:15-11:45, 12:30-16:00 .
There is work that takes time. For example, 90 minutes (or 1 hour: 30 minutes ).
It is necessary to find all possible intervals where the given work is placed.
From what comes to mind: split the working day into an array of intervals of 15 minutes (using DatePeriod and DateInterval ), loop through the array, adding work time to the beginning of the interval, and check the end time for occurrences in busy intervals.
But there will be quite a lot of such checks, which will affect performance.
Maybe there is some more elegant solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-02-23
@undefo

<?php
$begin = new DateTime('09:00');
$end = new DateTime('21:00');
$intervals = array(
  array(new DateTime('10:00'), new DateTime('10:15')),
  array(new DateTime('10:15'), new DateTime('11:45')),
  array(new DateTime('12:30'), new DateTime('16:00'))
);
$time = 90;

function checkInterval($begin, $end, $time) {
  $intervalDiff = $begin->diff($end);
  $intervalMinutes = $intervalDiff->h * 60 + $intervalDiff->i;
  if ($intervalMinutes >= $time) {
    echo $begin->format('H:i'), ' - ', $end->format('H:i'), "\n";
  }
}

$intervalStart = $begin;
foreach ($intervals as $interval) {
  checkInterval($intervalStart, $interval[0], $time);
  $intervalStart = $interval[1];
}
checkInterval($intervalStart, $end, $time);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question