计算x事件的重叠

The task is to calculate the overlap of events (stored in DB) and show the overlapping times as events. So if I have 3 Users and they have events spread through the day and there are times all 3 Users overlap I want to create an event representing that time and show it on the frontend.

Monday: User1 has Event 12:00-13:00 User2 has Event12:45-13:45 User3 has Event 12:30-14:00

Would give the overlapEvent 12:45-13:00

I have a database with the tables calendar and calendar_events. the calendar represents a user-calendar and calendar_events has all events. Events have the pid field to refer the calendar it belongs to. I have a method that returns all events for each day in the week. I have tried running over all events each day x times (x is the number of users, whose overlap I need to calculate) in for loops, but this seems very inefficient since the processing time increase exponentially with each user. I have tried creating a new array with the pid field as keys, but I don't really know where to go from here. I need to check each event against all events of that day from the other users.

$overlapEvents=array();
 foreach ($AllEvents as $key => $days) 
{
            $eventsSorted=array();
            foreach ($days as $day => $events) 
{

                foreach ($events as $event) 
{
                         array_push($eventSorted[$event['pid'],$event);

}
}
}

This is how $AllEvents looks like

Array
(
    [20190516] => Array //date of day 2019 05 16
        (
            [1558002600] => Array//event start time
                (
                    [0] => Array
                        (
                            [id] => 645
                            [pid] => 16
                            [tstamp] => 1557905849
                            [title] => testEvent
                            [alias] => testEvent
                            [author] => 1
                            [addTime] => 1
                            [startTime] => 1558002600
                            [endTime] => 1558003500
                            [startDate] => 1557957600
                            [endDate] => 1557957600
                            )
   )

)