2

Doing a personal project, making a calandar with just php. I simply cannot figure out a way to group arrays based on intersecting dates so that I can draw them on a grid. Something that looks like this

http://calendar.oregonstate.edu/docs/img/day_view_advanced.gif (cant post image yet)

original array

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)

assuming that there are several events most with over lapping dates. Here is the function I use to check if dates intersects and it works.

function Intersect ($date, $cDate, $start, $end) {
    return ($date == $start) || ($date > $start ? $date <= $end : $start < $cDate);
}

I don't have a problem with the actual drawing, but trying to group the arrays so that it places them appropriately on the grid.

    //$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;
            }
        }

    }

This groups ALL dates that intersect each other. This has been driving me crazy since I just can't figure out a way to split the $intersect array into different groups so that I can set the width correctly. optimally I would like the resulting array to look like this

    $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'
        )
    );

I know there a libraries that do this ie jquery full calendar plugin is pretty good but I'm trying to learn more about programming. Any help or links to point me at the right direction would be awesome.

0 Answers0