Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created July 15, 2014 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phpfiddle/5f5019326cb1894fda7c to your computer and use it in GitHub Desktop.
Save phpfiddle/5f5019326cb1894fda7c to your computer and use it in GitHub Desktop.
[ Posted by Fiddler ] Test for time overlap
<?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