识别日期和时间阵列的冲突

时间:2012-05-07 23:11:36

标签: php

我已经检查了其他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] =>冲突(当前会议) )。

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

3 个答案:

答案 0 :(得分: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`

其中current是包含当前计划的表,plan是包含用户计划的表。我之前没有测试过这个例子,因为我没有示例,但是这应该返回日期相同且plan次重叠current次的行。

编辑:如果不需要,您可能还希望将current标记为暂定。你可以把它放到查询where子句中。

此外,><表示开始和结束时间不包括在内。所以如果一个人在另一个人开始的同时结束,这不会将他们标记为冲突。如果您希望开始/结束分钟具有包容性,请将其更改为>=<=

答案 1 :(得分:1)

你可以这样做

$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

答案 2 :(得分: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!";
       }
     }
  }
}

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

另一个通知:比较时间,您需要将它们重新计算为分钟格式。