Created
July 15, 2014 21:01
-
-
Save phpfiddle/5f5019326cb1894fda7c to your computer and use it in GitHub Desktop.
[ Posted by Fiddler ] Test for time overlap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Function to test for time overlap | |
// $start_time A start date YYYY-MM-DD HH:MM:SS | |
// $end_time An end date YYYY-MM-DD HH:MM:SS | |
// $times An array of times to match against | |
// Returns true if there is an overlap false if no overlap is found | |
function time_overlap($start_time, $end_time, $times){ | |
$ustart = strtotime($start_time); | |
$uend = strtotime($end_time); | |
foreach($times as $time){ | |
$start = strtotime($time["start"]); | |
$end = strtotime($time["end"]); | |
if($ustart <= $end && $uend >= $start){ | |
return true; | |
} | |
} | |
return false; | |
} | |
$list_of_times = array( | |
array( | |
"start" => "2012-01-01 00:00:00", | |
"end" => "2012-01-30 00:00:00" | |
), | |
array( | |
"start" => "2012-02-01 00:00:00", | |
"end" => "2012-02-30 00:00:00" | |
), | |
array( | |
"start" => "2012-03-01 00:00:00", | |
"end" => "2012-03-30 00:00:00" | |
) | |
); | |
if(!time_overlap("2012-03-15 00:00:00", "2012-04-01 00:00:00", $list_of_times)){ | |
echo "No overlap found adding to array!<br />"; | |
$list_of_times[]["start"] = "2012-03-15 00:00:00"; | |
$list_of_times[]["end"] = "2012-04-01 00:00:00"; | |
}else{ | |
echo "Overlap found time not added to array!<br />"; | |
} | |
if(!time_overlap("2012-04-15 00:00:00", "2012-05-01 00:00:00", $list_of_times)){ | |
echo "No overlap found adding to array!<br />"; | |
$list_of_times[]["start"] = "2012-03-15 00:00:00"; | |
$list_of_times[]["end"] = "2012-04-01 00:00:00"; | |
}else{ | |
echo "Overlap found time not added to array!<br />"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment