I\'ve been looking forever for this, but the answer seems nowhere. Here\'s the problem:
Say, I\'ve got two date ranges.
$daterange1 = 2012-04-20 till
Here's an example using PHP's DateTime
class. Note that if you pass an invalid date string to DateTime::__construct()
the function will throw an exception, so you should implement a try/catch block if you're worried about this. Also, I use PHP's min
and max
functions so that it doesn't matter which order the dates are specified.
$daterange1 = array('2012-04-20', '2012-04-28');
$daterange2 = array('2012-04-18', '2012-05-01');
$range_min = new DateTime(min($daterange1));
$range_max = new DateTime(max($daterange1));
$start = new DateTime(min($daterange2));
$end = new DateTime(max($daterange2));
if ($start >= $range_min && $end <= $range_max) {
echo 'woot!';
} else {
echo 'doh!';
}
Though this is a very old post. But still if someone stuck. Here's complete example for overlapping dates.
<?php
$daterange1 = array('2017-09-24', '2017-09-28');
$daterange2 = array('2017-09-22', '2017-09-25');
$range_min = new DateTime(min($daterange1));
$range_max = new DateTime(max($daterange1));
$start = new DateTime(min($daterange2));
$end = new DateTime(max($daterange2));
if ($start >= $range_min && $end <= $range_max) {
echo 'Overlapping!';
}
else if($end > $range_min && $start < $range_max){
echo "partialy";
}
else {
echo 'free!';
}
?>
Well, logically you can break down the requirements for a range to be partly in another.
A range is only partly within another range if:
If either or both of those are true then the ranges are within each other.