RRule\RSet::getOccurrencesBetween PHP Method

getOccurrencesBetween() public method

Return all the ocurrences after a date, before a date, or between two dates.
public getOccurrencesBetween ( mixed $begin, mixed $end ) : array
$begin mixed Can be null to return all occurrences before $end
$end mixed Can be null to return all occurrences after $begin
return array An array of \DateTime objects
    public function getOccurrencesBetween($begin, $end)
    {
        if ($begin !== null) {
            $begin = RRule::parseDate($begin);
        }
        if ($end !== null) {
            $end = RRule::parseDate($end);
        } elseif ($this->isInfinite()) {
            throw new \LogicException('Cannot get all occurrences of an infinite recurrence rule.');
        }
        $iterator = $this;
        if ($this->total !== null) {
            $iterator = $this->cache;
        }
        $res = array();
        foreach ($iterator as $occurrence) {
            if ($begin !== null && $occurrence < $begin) {
                continue;
            }
            if ($end !== null && $occurrence > $end) {
                break;
            }
            $res[] = clone $occurrence;
        }
        return $res;
    }

Usage Example

Ejemplo n.º 1
0
 public function testCombineRRuleAndExDate()
 {
     $rset = new RSet();
     $rset->addRRule(array('FREQ' => 'YEARLY', 'COUNT' => 6, 'BYDAY' => 'TU, TH', 'DTSTART' => date_create('1997-09-02 09:00')));
     $rset->addExdate('1997-09-04 09:00:00');
     $rset->addExdate('1997-09-11 09:00:00');
     $rset->addExdate('1997-09-18 09:00:00');
     // adding out of order
     $this->assertEquals(array(date_create('1997-09-02 09:00'), date_create('1997-09-09 09:00'), date_create('1997-09-16 09:00')), $rset->getOccurrences());
     $this->assertEquals(date_create('1997-09-09 09:00'), $rset[1]);
     $this->assertEquals(array(date_create('1997-09-16 09:00')), $rset->getOccurrencesBetween('1997-09-16 00:00', '1997-09-17 00:00'));
     $this->assertTrue($rset->occursAt('1997-09-02 09:00'));
     $this->assertFalse($rset->occursAt('1997-09-04 09:00'));
     $rset->clearCache();
     $this->assertTrue($rset->occursAt('1997-09-02 09:00'));
     $this->assertFalse($rset->occursAt('1997-09-04 09:00'));
 }