php - Check if timeranges overlap -
i need check whether entered time range overlaps time range thread php function check time between given range? gives simple explanation on how check if 1 date range
i altered second example function/if-case following:
if(check_slot_range('2014-06-26 06:00:00','2014-06-26 10:00:00', '2014-06-26 07:00:00') or check_slot_range('2014-06-26 06:00:00','2014-06-26 10:00:00', '2014-06-26 09:00:00')){ echo "overlap"; }else{ echo "no overlap"; } only following give overlap:
range '2014-06-26 07:00:00' '2014-06-26 09:00:00'
range '2014-06-26 05:00:00' '2014-06-26 09:00:00'
range '2014-06-26 07:00:00' '2014-06-26 11:00:00'
this 1 not throw overlap:
- range '2014-06-26 05:00:00' '2014-06-26 11:00:00'
how need change if-clause catch overlap last example?
for clarification, here function compare
function check_slot_range($start_date, $end_date, $todays_date) { $start_timestamp = strtotime($start_date); $end_timestamp = strtotime($end_date); $today_timestamp = strtotime($todays_date); return (($today_timestamp >= $start_timestamp) && ($today_timestamp <= $end_timestamp)); }
to check overlap, check if low1 <= high2 , high1 >= low2 (instead of date >= low , date <= high in example above)
you can create new function checks condition this:
function check_ranges_overlap($start_date_1, $end_date_1, $start_date_2, $end_date_2) { $start_timestamp_1 = strtotime($start_date_1); $end_timestamp_1 = strtotime($end_date_1); $start_timestamp_2 = strtotime($start_date_2); $end_timestamp_2 = strtotime($end_date_2); return (($start_timestamp_1 <= $end_timestamp_2) && ($end_timestamp_1 >= $start_timestamp_2)); }
Comments
Post a Comment