/** * check if this date/time a range or equal to the end dates */ public function isWithinOrEqual(A_Datetime_Range $range) { return $this->getTimestamp() >= $range->getStart() && $this->getTimestamp() <= $range->getEnd(); }
/** * Checks whether or not the given Range object intersects this range * * @param A_Datetime_Range $range Range object to check * @return bool */ public function intersects($range) { if (!$range || $this->end->getTimestamp() < $range->getStart()->getTimestamp() || $range->getEnd()->getTimestamp() < $this->start->getTimestamp()) { return false; } return true; }
function testIntersects() { $datetime1 = new A_Datetime(); $datetime1->parseDate('2001/01/01'); $datetime2 = new A_Datetime(); $datetime2->parseDate('2001/02/01'); $datetime3 = new A_Datetime(); $datetime3->parseDate('2001/03/01'); $datetime4 = new A_Datetime(); $datetime4->parseDate('2001/04/01'); $datetime5 = new A_Datetime(); $datetime5->parseDate('2001/05/01'); // not overlapping $range1 = new A_Datetime_Range($datetime1, $datetime2); $range2 = new A_Datetime_Range($datetime3, $datetime4); $this->assertFalse($range1->intersects($range2)); // range1 end date in range2 $range1 = new A_Datetime_Range($datetime1, $datetime3); $range2 = new A_Datetime_Range($datetime2, $datetime4); $this->assertTrue($range1->intersects($range2)); // range1 start date in range2 $range1 = new A_Datetime_Range($datetime3, $datetime5); $range2 = new A_Datetime_Range($datetime1, $datetime4); $this->assertTrue($range1->intersects($range2)); // range1 the same as range2 $range1 = new A_Datetime_Range($datetime2, $datetime4); $range2 = new A_Datetime_Range($datetime2, $datetime4); $this->assertTrue($range1->intersects($range2)); // range1 end date the same as range2 start date $range1 = new A_Datetime_Range($datetime1, $datetime3); $range2 = new A_Datetime_Range($datetime3, $datetime5); $this->assertTrue($range1->intersects($range2)); // range1 start date the same as range2 end date $range1 = new A_Datetime_Range($datetime3, $datetime5); $range2 = new A_Datetime_Range($datetime1, $datetime3); $this->assertTrue($range1->intersects($range2)); // range1 inside range2 $range1 = new A_Datetime_Range($datetime2, $datetime4); $range2 = new A_Datetime_Range($datetime1, $datetime5); $this->assertTrue($range1->intersects($range2)); // range2 inside range1 $range1 = new A_Datetime_Range($datetime1, $datetime5); $range2 = new A_Datetime_Range($datetime2, $datetime4); $this->assertTrue($range1->intersects($range2)); }