Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created February 17, 2020 11:36
Show Gist options
  • Save kobus1998/a6bdd3232c1f6b25e949e6ac233c2da4 to your computer and use it in GitHub Desktop.
Save kobus1998/a6bdd3232c1f6b25e949e6ac233c2da4 to your computer and use it in GitHub Desktop.
check periods crosses another period
<?php
class Period extends \DatePeriod
{
public function crosses(\DatePeriod $period)
{
return $this->numbers_cross(
strtotime($this->getStartDate()->format('Y-m-d')),
strtotime($this->getEndDate()->format('Y-m-d')),
strtotime($period->getStartDate()->format('Y-m-d')),
strtotime($period->getEndDate()->format('Y-m-d'))
);
}
private function numbers_cross($min1, $max1, $min2, $max2)
{
return ($min1 >= $min2 && $min1 <= $max2) || ($max1 <= $max2 && $max1 >= $min2);
}
}
function getPeriod($sFrom, $sTo)
{
try {
$oPeriod = new \Period(
new \DateTime(date("Y-m-d", strtotime($sFrom))),
new \DateInterval('P1D'),
new \DateTime(date("Y-m-d", (strtotime($sTo))))
);
} catch (\Exception $e) {
return null;
}
return $oPeriod;
}
$p1 = getPeriod('01-01-2020', '03-01-2020');
$p2 = getPeriod('02-01-2020', '04-01-2020');
echo $p1->crosses($p2) ? "yes" : 'no'; // yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment