Created
January 23, 2013 14:18
-
-
Save benjaminrau/4606121 to your computer and use it in GitHub Desktop.
Compare two DateInterval objects with dynamic operator
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
/** | |
* Compare Date Intervals | |
* | |
* @param DateInterval $dateInterval1 | |
* @param DateInterval $dateInterval2 | |
* @param string $operator | |
* @return boolean | |
*/ | |
private function compareDateIntervals(DateInterval $dateInterval1, DateInterval $dateInterval2, $operator) { | |
$newDate = new DateTime(); | |
$newDate2 = clone $newDate; | |
switch ($operator) { | |
case "==": return $newDate->add($dateInterval1) == $newDate2->add($dateInterval2); | |
case "!=": return $newDate->add($dateInterval1) != $newDate2->add($dateInterval2); | |
case ">=": return $newDate->add($dateInterval1) >= $newDate2->add($dateInterval2); | |
case "<=": return $newDate->add($dateInterval1) <= $newDate2->add($dateInterval2); | |
case ">": return $newDate->add($dateInterval1) > $newDate2->add($dateInterval2); | |
case "<": return $newDate->add($dateInterval1) < $newDate2->add($dateInterval2); | |
default: return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment