Finding if time overlaps

I’m trying to create a function to find out if two time ranges overlap.

function($from, $to, $day, $from_compare, $to_compare, $day_compare)

$from, $to, $from_compare, $to_compare should get a 24-hour time format, for example, 13:00-17:00

$day and $day_compare would contain something like Monday or Tuesday, but not a specific date.

I’ve been googling it but can’t find a good solution. Any suggestions? I’m working with 24-hour format. Also, the function needs to take into account the following scenario: 18:00-02:00 (6pm-2am)?

Any suggestions would be appreciated