做个人项目,用php制作一个calandar。我根本无法想出一种基于相交日期对数组进行分组的方法,以便我可以在网格上绘制它们。看起来像这样的东西
http://calendar.oregonstate.edu/docs/img/day_view_advanced.gif(无法发布图片)
原始数组
array
0 =>
array (size=7)
'ID' => string '6188' (length=4)
'name' => string 'test' (length=32)
'dt_start' => string '2012-10-04 10:00:00' (length=19)
'dt_end' => string '2012-10-04 11:00:00' (length=19)
假设有多个事件多数具有重叠日期。这是我用来检查日期是否相交并且有效的函数。
function Intersect ($date, $cDate, $start, $end) {
return ($date == $start) || ($date > $start ? $date <= $end : $start < $cDate);
}
我对实际绘图没有问题,但尝试对数组进行分组,以便将它们适当地放在网格上。
//$e is the events
foreach ($e as $k => $a) {
$oStart1 = new DateTime( $a ['dt_start'] );
$oEnd1 = new DateTime( $a ['dt_end'] );
foreach ($e as $i => $b) {
if ( $a['ID'] == $b['ID'] )
continue;
$oStart2 = new DateTime( $b ['dt_start'] );
$oEnd2 = new DateTime( $b ['dt_end'] );
if ( Intersect ($oStart1, $oEnd1, $oStart2, $oEnd2) ) {
$intersect[$a['ID']] = $a;
}
}
}
将所有相互交叉的日期分组。这让我抓狂,因为我无法找到将$ intersect数组拆分成不同组的方法,以便我可以正确设置宽度。最佳的我希望结果数组看起来像这样
$group[ 1 ] = array (
array (
'start_time' => '2012-10-04 10:00:00',
'end_time' => '2012-10-04 12:00:00'
),
array (
'start_time' => '2012-10-04 10:30:00',
'end_time' => '2012-10-04 11:40:00'
),
array (
'start_time' => '2012-10-04 11:00:00',
'end_time' => '2012-10-04 12:00:00'
)
);
$group[ 2 ] = array (
array (
'start_time' => '2012-10-04 13:00:00',
'end_time' => '2012-10-04 14:00:00'
),
array (
'start_time' => '2012-10-04 13:30:00',
'end_time' => '2012-10-04 13:40:00'
)
);
我知道有一个库可以做到这一点,即jquery完整的日历插件非常好,但我正在努力学习有关编程的更多信息。任何指向正确方向的帮助或链接都会很棒。