php中的多个时间交集

时间:2011-08-17 16:58:40

标签: php time overlapping

我有一种情况,我现在无法绕过头。多次重叠。我在两个times that overlap上看过很多帖子,但没有多次重叠的帖子。

以下是我想要提供的数据:

<?php
//RUN EACH Class Selection for Monday
foreach($class as $Classes){
    $time[] = array($ClassStartTime, $ClassEndTime, $classID);
}

OverLapFunction($time);
?>

然后发布classID和Overlapping Amount。有没有人遇到过这种情况?或者弄清楚如何做到这一点?

2 个答案:

答案 0 :(得分:3)

说你的数据是这样的:

$classes = array(
    array(
       'name' => 'A',
       'day'  => 'Monday',
       'start'=> '08:00AM',
       'end'  => '11:00AM',
    ),
    array(
       'name' => 'B',
       'day'  => 'Monday',
       'start'=> '10:00AM',
       'end'  => '11:30AM',
    ),
    array(
       'name' => 'C',
       'day'  => 'Monday',
       'start'=> '12:00PM',
       'end'  => '04:00PM',
    ),
    array(
       'name' => 'D',
       'day'  => 'Monday',
       'start'=> '03:00PM',
       'end'  => '06:00PM',
    ),
);

你只需要一个嵌套的foreach,如下所示:

$overlap = array();
foreach ($classes as $class1) {
    foreach ($classes as $class2) {
        if ($class1['day'] != $class2['day'] || $class1 == $class2) continue;
        if (strtotime($class1['start']) <  strtotime($class2['end']) &&
            strtotime($class1['start']) >= strtotime($class2['start']))
        {
            $array = array($class1['name'], $class2['name']);
            sort($array);
            if (!in_array($array, $overlap)) $overlap[] = $array;
        }
    }
}

基本上,它将每个类与所有类进行比较。如果$class1开始时间小于$class2结束时间 $class1,则开始时间超过$class2开始时间:它们会重叠。请记住,每个类将相互比较两次(例如:A到B,B到A),所以如果它在第一遍中不匹配,它将在第二遍。

这会给你:

Array
(
    [0] => Array
        (
            [0] => B
            [1] => A
        )

    [1] => Array
        (
            [0] => D
            [1] => C
        )

)

如果您要将班级D更改为:

array(
   'name' => 'D',
   'day'  => 'Monday',
   'start'=> '10:00AM',
   'end'  => '03:00PM',
),

...有效地重叠所有课程,你会得到:

Array
(
    [0] => Array
        (
            [0] => A
            [1] => B
        )

    [1] => Array
        (
            [0] => B
            [1] => D
        )

    [2] => Array
        (
            [0] => C
            [1] => D
        )

    [3] => Array
        (
            [0] => A
            [1] => D
        )

)

答案 1 :(得分:0)

免责声明:此功能更多伪码,您需要自己实现;它返回重叠类的组。我这样写的就是让你更好地理解它。如果您需要更多解释为什么我们需要先对时间进行排序,请告诉我。

function OverLapFunction($time) {
    sort_by_start_time($time); // ensures that we don't have
                               // overlapping groups at the end
    $groups = array(); // groups have starting time, ending
                       // time and items they contain
    foreach($time as $item) {
        // if we don't find a group to put this item in, we create a new one
        $found = false;
        foreach($groups as $group) {
            // basically the whole if statement looks for overlapping between
            // the group and the current item
            if(($item['start_time'] >= $group['start_time'] 
                && $item['start_time'] <= $group['end_time']) ||
                ($item['end_time'] >= $group['start_time']
                &&  $item['end_time'] <= $group['end_time']))
            $group['items'][] = $item; // add the item to its group
            $found = true;
            break;
        }
        if(!$found) { // no group that fits this item; create a new one
            $groups[] = array(
                'start_time' => $item['start_time'], // start and end times
                'end_time' => $item['end_time'],     // are the item's
                'items' => array($item),
            );
        }
    }
    return $groups;
}

哦,并且为了记录:如果你有一个用户选择了一些类,你需要确保他所选择的类中没有两个重叠,你可以将问题减少到你已经掌握的那个:

for($i=0; $i<count($item); $i++) {
    for($j=0; $j<count($item); $j++) {
        if($i == $j) {
            continue;
        }   
        if(times_overlap($item[$i], $item[$j])) {
            do_something(); // the user has chosen incorrect classes
        }
    }
}

注意:如果您通过开始日期对项目进行排序并检查两个连续的数组元素是否重叠,则可以更好地提高上述问题。