public function overlap(DayTime $b) { // Seconds from commencement of the week; $selfMinutes = $this->getDay() * self::MINUTES_IN_DAY + $this->getHour() * self::MINUTES_IN_HOUR + $this->getMinute(); $bMinutes = $b->getDay() * self::MINUTES_IN_DAY + $b->getHour() * self::MINUTES_IN_HOUR + $b->getMinute(); if ($selfMinutes > $bMinutes) { // I start before the other does return min($bMinutes + $b->getDuration(), $selfMinutes + $this->getDuration()) - $selfMinutes; } // Does this DayTime end overlap with the $b period? if ($selfMinutes + $this->getDuration() < $bMinutes) { // No overlap - return 0; return 0; } $selfEnd = $selfMinutes + $this->getDuration(); $bEnd = $bMinutes + $b->getDuration(); if ($selfEnd < $bEnd) { // I end before $b ends return $bMinutes + ($bEnd - $selfEnd); } return $b->getDuration(); }