1

我必须使用特定的时间计划检查mysql中的一些时间重叠,例如:

time_plan1 = 2011-06-30, startTime = 09:00:00, finishTime = 10:00:00
time_plan2 = 2011-06-30, startTime = 12:30:00, finishTime = 13:30:00
time_plan3 = 2011-06-30, startTime = 14:00:00, finishTime = 15:30:00
time_plan4 = 2011-06-30, startTime = 17:00:00, finishTime = 18:00:00

和时代

time1 = 2011-06-30, startTime = 08:30:00, finishTime = 10:00:00
time2 = 2011-06-30, startTime = 12:30:00, finishTime = 13:30:00
time3 = 2011-06-30, startTime = 14:00:00, finishTime = 15:00:00
time4 = 2011-06-30, startTime = 16:30:00, finishTime = 19:00:00

所以我必须为每两个时间计划和时间找到时间计划和时间之间的重叠,但我无法指定哪个时间属于哪个时间计划

更新

对于结果,我需要在这种情况下获得属于时间计划的时间总和:time1:1h time2:1h time3:1h time4:1h sum for 2011-06-30:4h

4

2 回答 2

2
WHERE time1.startTime > time_plan1.startTime and time1.finishTime < time_plan1.finishTime

这几乎是正确的,但是您只会遇到时间开始晚于时间计划,而时间结束时间早于时间计划的情况。

为了得到重叠,

WHERE time1.startTime < time_plan1.startTime or time1.finishTime > time_plan1.finishTime

这样做是捕捉这 3 个重叠的场景

  • 时间早于 startTime 开始,但晚于 finishTime 结束
  • 时间早于 startTime 开始,也早于 finishTime
  • time 开始于 startTime 之后,但结束于 finishTime 之后

棘手的部分是获得重叠的总和,我认为您需要这样做,PHP因为由sql.

假设您已收到包含时间和时间计划的 startTime 和 finishTime 字段的记录。

$sum = 0; // seconds
foreach ($records as $record) {
  $overlap_start = max($record->time_startTime, $record->timeplan_startTime);
  $overlap_finish = min($record->time_finishTime, $record->timeplan_finishTime);
  $sum += strtotime($overlap_finish) - strtotime($overlap_start);
}
于 2013-06-27T15:51:38.040 回答
1

谢谢王牌你的回答帮助我找到了答案..保存在历史中

foreach($plan as $wpk=>$pla){
       foreach($work as $wtk=>$wor){
        if($wor['start']<$pla['end'] and $wor['end']>$pla['start']){
          $overlap_start = max($wor['start'],$pla['start']);
          $overlap_finis = min($wor['end'], $pla['end']);
          $sum +=  $overlap_finis - $overlap_start;
          if($overlap_start>$wor['start'])
          $work[] = array('start'=>$wor['start'],'end'=>$overlap_start);
          if($wor['end']>$overlap_finis)
          $work[] = array('start'=>$overlap_finis,'end'=>$wor['end']);
          unset($work["$wtk"]);
                     }
                   }
于 2013-06-29T22:02:45.883 回答