Skip to content

Instantly share code, notes, and snippets.

@lshaf
Created February 19, 2016 13:42
Show Gist options
  • Save lshaf/23c2d57ac8961c71254b to your computer and use it in GitHub Desktop.
Save lshaf/23c2d57ac8961c71254b to your computer and use it in GitHub Desktop.
Script to test date range overlap
<?php
echo '<pre>';
$sDate = '2016-02-03';
$eDate = '2016-02-10';
$overlapTest = [
# start before sDate
['2016-02-01', '2016-02-03'],
['2016-02-01', '2016-02-02'],
['2016-02-01', '2016-02-09'],
['2016-02-01', '2016-02-11'],
# start in middle range
['2016-02-04', '2016-02-06'],
['2016-02-04', '2016-02-10'],
['2016-02-04', '2016-02-11'],
# start in the end date
['2016-02-10', '2016-02-11'],
['2016-02-11', '2016-02-12'],
];
foreach ($overlapTest as $date){
$cond = ($sDate <= $date[1] and $eDate >= $date[0]);
echo "{$date[0]} - {$date[1]} = ".($cond ? 'overlap' : 'pass').PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment