1

I'm using while to print events on certain date with eventID, start and end time.

How can I check and print which event time overlaps with which using PHP?

<?php
while (list($key, $event_row) = each($events)) {
    $times_array = array(
        $event_row[0],
        date('Y-m-d H:i:s', $event_row[1]),
        date('Y-m-d H:i:s', $event_row[2])
    );
    print_r($times_array);
}

Array ( [0] => 11 [0] => 2015-05-29 19:00:00 [1] => 2015-05-29 21:00:00 )
Array ( [0] => 13 [0] => 2015-05-29 19:00:00 [1] => 2015-05-29 21:00:00 )
Array ( [0] => 16 [0] => 2015-05-29 21:00:00 [1] => 2015-05-29 22:00:00 )

Example output I would like is:

Event ID#: 11 overlaps with Event ID# 13.
Event ID#: 13 overlaps with Event ID# 11.
Event ID#: 16 doesn't overlap.
Salman A
  • 262,204
  • 82
  • 430
  • 521
hulkster
  • 49
  • 6
  • 3
    1. What is your question/problem ? 2. What output do you get and what would you expect? – Rizier123 May 25 '15 at 09:23
  • Can you post `var_export($events)`? – Salman A May 25 '15 at 10:07
  • array ( 0 => array ( 0 => '11', 1 => 1432918800, 2 => 1432926000, ), 1 => array ( 0 => '13', 1 => 1432918800, 2 => 1432926000, ), 2 => array ( 0 => '16', 1 => 1432926000, 2 => 1432929600, ), ) – hulkster May 25 '15 at 10:36

1 Answers1

4

Refer to the generic answer for checking if two date ranges overlap. For PHP, you need to compare each event with all other events and check for conflicts by testing:

EndDate2 > StartDate1 AND EndDate1 > StartDate2

Example code:

<?php
$events = array(
    array("11", 1432918800 /*2015-05-29 19:00:00*/, 1432926000 /*2015-05-29 21:00:00*/),
    array("13", 1432918800 /*2015-05-29 19:00:00*/, 1432926000 /*2015-05-29 21:00:00*/),
    array("16", 1432926000 /*2015-05-29 21:00:00*/, 1432929600 /*2015-05-29 22:00:00*/)
);
foreach ($events as $thisevent) {
    $conflicts = 0;
    foreach ($events as $thatevent) {
        if ($thisevent[0] === $thatevent[0]) {
            continue;
        }
        $thisevent_from = $thisevent[1];
        $thisevent_ends = $thisevent[2];
        $thatevent_from = $thatevent[1];
        $thatevent_ends = $thatevent[2];
        if ($thatevent_ends > $thisevent_from AND $thisevent_ends > $thatevent_from) {
            $conflicts++;
            echo "Event #" . $thisevent[0] . " overlaps with Event # " . $thatevent[0] . "\n";
        }
    }
    if ($conflicts === 0) {
        echo "Event #" . $thisevent[0] . " is OK\n";
    }
}
Community
  • 1
  • 1
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • But how can I use this function with my events array variables if there are more than three event arrays? Thank you. – hulkster May 25 '15 at 10:43
  • I have posted sufficient amount of code. Improvise. – Salman A May 25 '15 at 10:44
  • I created new events array like that but it returns all is OK: $events[] = array("id" =>$event_row[0], "from" => date('Y-m-d H:i:s',$event_row[1]), "ends" => date('Y-m-d H:i:s',$event_row[2])); – hulkster May 25 '15 at 10:53
  • I changed my answer to match your `var_export`ed input. See if it works. – Salman A May 25 '15 at 10:54