Both the starting and endings times can be either inside or outside the window. That makes four combinations:
1. The starting time is within the window and the ending time is not ($window_end - $start_time)
2. The starting time is before the window and the ending time is within ($end_time - $window_start)
3. Both starting and ending times are within the window ($end_time - $start_time)
4. Both starting and ending times are outside the window ($window_end - $window_start)
Code: Select all
window_start window_end
| |
1 | start_time-------+---end_time
| |
2 start_time---+---------end_time |
| |
3 |start_time--end_time|
| |
4 start_time---+--------------------+---end_time
| |
If you look carefully you'll see that the logic can be reduced to a simple
Code: Select all
min($window_end, $end_time) - max($window_start, $start_time)
Take care with overflow like as with the 1800-0600 window: it'll be easier to treat that like 1800-3000.