4

我检查了其他 SO 问题,但似乎找不到与此相关的任何内容 - 如果我错过了什么,请指出我正确的方向。

我目前有以下数组,其中包含日期和时间(要求的会议时间):

Array (
    [0] =>
        day => 'Thursday',
        start_time => '14:00',
        end_time => '15:00'
    [1] =>
        day => 'Thursday',
        start_time => '16:30',
        end_time => '17:30'
)

用户必须参加这两个时间。我试图做的是与另一个包含用户当前约会的数组进行比较,并检测是否存在冲突/时间重叠问题

Array (
    [0] =>
        day => 'Monday',
        start_time => '09:00',
        end_time => '13:00'
    [1] =>
        day => 'Wednesday',
        start_time => '10:45',
        end_time => '11:15'
    [2] =>
        day => 'Thursday',
        start_time => '16:45',
        end_time => '17:45'
)

使用上面的示例,我想确定与[1] =>第一个数组的第二个时间段(提议的会议)发生冲突,该时间[2] =>段与第二个数组的第三个时间段(当前会议)发生冲突。

我见过带有时间戳和完整日期的示例,但不仅仅是日期和开始/结束时间。

4

3 回答 3

2

如果它们都在数据库中,则冲突查询可能如下所示:

SELECT `plan`.*
FROM `plan`
    JOIN `current` ON `plan`.`day`=`current`.`day`
WHERE
    `plan`.`start_time` < `current`.`end_time`
    AND `plan`.`end_time` > `current`.`start_time`

wherecurrent是当前日程表,plan是用户日程表。我没有测试这个只是因为我没有一个例子,但这应该返回日期相同且plan时间重叠current时间的行。

编辑:如果不需要,您可能还想做标记current为暂定之类的东西。您可以将其放入查询 where 子句中。

也意味着开始和结束分钟不包括在内><因此,如果一个在另一个开始的同时结束,这不会将它们标记为冲突。如果您希望开始/结束分钟包含在内,请将它们更改为>=and <=

于 2012-05-08T00:13:06.703 回答
2

你可以这样做

$slot1 = Array (
        0 => array (
                'day' => 'Thursday',
                'start_time' => '14:00',
                'end_time' => '15:00' 
        ),
        1 => array (
                'day' => 'Thursday',
                'start_time' => '16:30',
                'end_time' => '17:30' 
        ) 
);

$slot2 = Array (
        0 => array (
                'day' => 'Monday',
                'start_time' => '09:00',
                'end_time' => '13:00' 
        ),
        1 => array (
                'day' => 'Wednesday',
                'start_time' => '10:45',
                'end_time' => '11:15' 
        ),
        2 => array (
                'day' => 'Thursday',
                'start_time' => '16:45',
                'end_time' => '17:45' 
        ) 
);

echo "<pre>";
$format = "l H:s";
foreach($slot1 as $value)
{
    $slot1Start = DateTime::createFromFormat ($format , $value ['day'] . " " . $value['start_time'] );
    $slot1End = DateTime::createFromFormat ( $format , $value ['day'] . " " . $value['end_time'] );

    foreach($slot2 as $value2)
    {
        $slot2Start = DateTime::createFromFormat ( $format , $value2 ['day'] . " " .$value2['start_time'] );
        $slot2End = DateTime::createFromFormat ( $format , $value2 ['day'] . " " .$value2 ['end_time'] );


        if ($slot1Start <= $slot2End && $slot1End >= $slot2Start) {
            echo sprintf ( "Even Overlap on %s %s-%s AND %s %s-%s", $value ['day'], $value ['start_time'], $value ['end_time'],
                    $value2 ['day'], $value2 ['start_time'], $value2 ['end_time'] ), PHP_EOL;
        }

    }
}

输出

Even Overlap on Thursday 16:30-17:30 AND Thursday 16:45-17:45
于 2012-05-08T00:20:13.653 回答
0

你有一些修改过的数组代码

   $current_sheduler = Array (
        [0] =>
            day => 'Thursday',
            start_time => '14:00',
            end_time => '15:00'
        [1] =>
            day => 'Thursday',
            start_time => '16:30',
            end_time => '17:30'
    )

    $user_plans = Array (
        [0] =>
            plan_name => 'event1',
            day => 'Monday',
            start_time => '09:00',
            end_time => '13:00'
        [1] =>
            plan_name => 'event2',
            day => 'Wednesday',
            start_time => '10:45',
            end_time => '11:15'
        [2] =>
            plan_name => 'event3',
            day => 'Thursday',
            start_time => '16:45',
            end_time => '17:45'
    )

如果用户计划的事件与您的第一个数组的时间匹配,这是处理您的数组和捕获的方法

foreach($user_plans as $plan){
  foreach($current_sheduler as current){
     if($plan['day']==$current['day']){
       if(
         ($plan['start_time']>=$current['start_time']) &&
         ($plan['end_time']<=$current['end_time'])
       ){
          echo "Event {$plan['plan_name']} is working now!";
       }
     }
  }
}

这是一般逻辑,对于情况,匹配仅表示全时段匹配。

另一个注意事项:在比较时间之前,您需要将它们重新计算为计数分钟格式。

于 2012-05-07T23:28:45.957 回答