How to calculate date range between multiple date that overlaps

982 views Asked by At

i have many dates that i need to check.

For example:

1 - Start: 1387267200, End: 1387274400
2 - Start: 1387270800, End: 1387275000 
3 - Start: 1387250200, End: 1387273000 
4 - Start: 1387285200, End: 1387288800

What i need is to calculate foreach overlaps the minimum start date and maximum end date.

For the example above, what the algor has to give in output is:

Start1: 1387250200 -> End1: 1387275000.
Start2: 1387285200 -> End2: 1387288800.

How can i do it?

Thanks!

2

There are 2 answers

1
Carlos On BEST ANSWER

This script looks for overlapped intervals and gets new ones with minimum start date and maximum end date:

// Input
$intervals = array(
    array(1387267200, 1387274400),
    array(1387270800, 1387275000),
    array(1387250200, 1387273000),
    array(1387285200, 1387288800),
);
// Overlapped intervals
$overlapped = array();
foreach ($intervals as $i => $a) {
    $group = [$a];
    foreach ($intervals as $j => $b) {
        if (
            $i !== $j && (($a[0] <= $b[0] && $a[1] >= $b[0])
                || ($a[0] <= $b[1] && $a[0] >= $b[0]))
        ) {
            $group[] = $b;
        }
    }
    sort($group);
    $overlapped[] = $group;
}
// Multidimensional array_unique()
$overlapped = array_map('unserialize',
    array_unique(array_map('serialize', $overlapped))
);
// Output
$output = array();
// Get min/max dates for each overlapped group
foreach ($overlapped as $group) {
    $min = null;
    $max = null;
    foreach ($group as $interval) {
        if ($min === null || $interval[0] < $min) {
            $min = $interval[0];
        }
        if ($max === null || $interval[1] > $max) {
            $max = $interval[1];
        }
    }
    $output[] = array($min, $max);
}
print_r($output);

Output:

Array
(
    [0] => Array
        (
            [0] => 1387250200
            [1] => 1387275000
        )

    [1] => Array
        (
            [0] => 1387285200
            [1] => 1387288800
        )

)
0
Abhishek Bansal On

Sort all the start and end times in an array.

  1. Iterate through the array.
  2. Set i = 0; and count = 0; (count keeps track of the number of active intervals)
  3. When you encounter a start time, note it down as the start time of i'th overlap. Also, increment the counter count.
  4. When you encounter an end time, decrement the count. If the count becomes zero, note this as the end time of ith interval and increment i.