What are the potential pitfalls of counting overlapping time intervals in PHP?

When counting overlapping time intervals in PHP, one potential pitfall is double-counting periods that overlap with multiple intervals. To solve this issue, we can iterate through each interval and check if it overlaps with any other intervals. If it does, we can adjust the overlapping period to avoid double-counting.

function countOverlapIntervals($intervals) {
    $count = 0;
    
    foreach ($intervals as $key => $interval) {
        $overlap = $interval;
        
        foreach ($intervals as $key2 => $interval2) {
            if ($key != $key2 && $interval[1] > $interval2[0] && $interval[0] < $interval2[1]) {
                $overlap[0] = max($interval[0], $interval2[0]);
                $overlap[1] = min($interval[1], $interval2[1]);
            }
        }
        
        $count += $overlap[1] - $overlap[0];
    }
    
    return $count;
}

$intervals = [[1, 5], [3, 7], [6, 10]];
echo countOverlapIntervals($intervals); // Output: 7