日历,偏移重叠事件

时间:2009-05-20 22:32:38

标签: php arrays prototype calendar offset

我正在尝试用PHP编写日历。在周视图中,我希望我的事件被列为iCal,其中同步事件将其宽度减小到一半。

我很难想出这个,所以我希望你能帮助我。我想要的是,如果一个事件与另一个事件重叠,它应该在两个事件数组上设置[split] => true - 或者在那个方向上设置一些东西(读:我不确定这是否是最有效的解决方案)。然后我可以在foreach循环中检查split == true,打印出事件。

这是一个包含两个同时发生的事件的示例数组:

$events = array(
  array(
    "id" => 21,
    "start" => 1242219600,
    "end" => 1242237600,
    "title" => "foo",
    "split" => false
  ),
  array(
    "id" => 22,
    "start" => 1242223200,
    "end" => 1242234000,
    "title" => "foo",
    "split" => false
  )
);

$events = someFunctionToOffsetEvents($events);

你会如何解决这个问题?

4 个答案:

答案 0 :(得分:1)

我最近不得不处理日期碰撞问题,我能想到的最好的是:

date1.start< date2.end和date1.end> date2.start = collision

这个简单的公式将解释以下所有情况:

--same for all situations:
date1.start = 1/1/2009
date1.end = 1/10/2009

--Start date is in first dates range:
date2.start = 1/9/2009
date2.end = 2/10/2009

--End date is in first dates range:
date2.start = 12/10/2008
date2.end = 1/3/2009

--Start & End date is inside first dates range:
date2.start = 1/2/2009
date2.end = 1/3/2009

--First date is inside of second dates range:
date2.start = 12/1/2008
date2.end = 2/1/2009
$date1 = array('start' => '2009-01-05', 'end' => '2009-01-10');
$date2 = array('start' => '2009-01-01', 'end' => '2009-01-04'); // end inside one
$date3 = array('start' => '2009-01-04', 'end' => '2009-01-15'); // start inside one
$date4 = array('start' => '2009-01-01', 'end' => '2009-01-15'); // one inside me
$date5 = array('start' => '2009-01-04', 'end' => '2009-01-05'); // inside one

function datesCollide($date1, $date2)
{
    $start1TS = strtotime($date1['start']);
    $end1TS = strtotime($date1['end']);
    $start2TS = strtotime($date2['start']);
    $end2TS = strtotime($date2['end']);

    if ($start1TS <= $end2TS && $end1TS >= $start2)
    {
        return true;
    }

    return false;
}

根据您的评论,这可能是您正在寻找的解决方案: 请注意,此解决方案不是非常优化,只应用于找出更好的解决方案。 绝不相信来自网络的代码。

$events = array(
  array(
        "id" => 21,
        "start" => 1242219600,
        "end" => 1242237600,
        "title" => "foo",
        "split" => false
  ),
  array(
        "id" => 22,
        "start" => 1242223200,
        "end" => 1242234000,
        "title" => "foo",
        "split" => false
  )
);

foreach ($events as $key => $event)
{
    $events[$key]->split = dateCollisionCheck($event, $events);
}

function dateCollisionCheck(&$event, &$eventList)
{
    foreach ($eventList as $checkEvent)
    {
        if ($event->id != $checkEvent->id)
        {
            if ($event->start <= $checkEvent->end && $event->end >= $checkEvent->start)
            {
                return true; // return as soon as we know there is a collision
            }
        }
    }

    return false;
}

*代码尚未经过测试

答案 1 :(得分:1)

我发布了关于如果三个事件重叠会导致更好的解决方案的问题。不要将GUI和数据放在同一个结构中。保存数据,然后找出如何显示它。

答案 2 :(得分:0)

我会恢复你的问题。 将事件映射到表示要显示的周的数组:

$weekdayEvents=array();
foreach ($events as $event) {
   $day=toThisWeek($event['start'], $firstweekday),
   $lastDay=toThisWeek($event['end'], $firstweekday); 
   while ($day<=$lastDay) {
     $weekdayEvents[$day++][]=$event['id'];
   }
}
// now you have an array of array filled with ids
$eventCollisions=array();
foreach($weekdaysEvents as $activeDay) {
  $eventsOnDay = count($activeDay);
  foreach($activeDay as $eventID) {
    if ($eventCollisions[$eventID]<$eventsOnDay) {
      $eventCollisions[$eventID]=$eventsOnDay;
    }
  }
}

}

其中toThisWeek()是一个类似

的函数
function toThisWeek($dayTimestamp, $firstDayOfWeekTimestamp) {
  $dayOfWeek = unixtojd($firsDayOfWeekTimestamp) - unixtojd($dayTimestamp);
  if($dayOfWeek<0) return 0;
  if($dayOfWeek>6) return 6; 
  return $dayOfWeek;      
}

现在在 $ eventCollisions 中,您必须在必须显示的一周内发生的每个事件的事件发生最大冲突次数。现在,您可以使用此数组中的值来填充$ events子阵列中的字段:

foreach ($events as $event) {
  $event['split']=$eventCollisions[$event['id']]-1;
}

或者只使用$ eventCollisions “ondemand”中的值

答案 3 :(得分:0)

以下是基于Rob的回答的工作修改代码:

// this is in my getEvents function
if(is_array($events)) {
  foreach ($events as $key => $event) {
    $events[$key]['collides'] = $this->dateCollisionCheck($event, $events);
  }
}

//this is another function added to calendar.class.php
function dateCollisionCheck(&$event, &$eventList) {
  foreach ($eventList as $checkEvent) {
    if ($event['id'] != $checkEvent['id']) {
      if ($event['start'] <= $checkEvent['end'] && $event['end'] >= $checkEvent['start']) {
        return true;
      }
    }
  }

  return false;
}