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.
3
  • 3
    1. What is your question/problem ? 2. What output do you get and what would you expect? Commented May 25, 2015 at 9:23
  • Can you post var_export($events)? Commented May 25, 2015 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, ), ) Commented May 25, 2015 at 10:36

1 Answer 1

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";
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

But how can I use this function with my events array variables if there are more than three event arrays? Thank you.
I have posted sufficient amount of code. Improvise.
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]));
I changed my answer to match your var_exported input. See if it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.