RRule\RRule::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 = self::parseDate($begin);
        }
        if ($end !== null) {
            $end = self::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

Example #1
0
 public function testDateTimeMutableReferenceBug()
 {
     $date = date_create('2007-01-01');
     $rrule = new RRule(array('freq' => 'daily', 'count' => 10, 'dtstart' => $date));
     $this->assertEquals(date_create('2007-01-01'), $rrule[0]);
     $date->modify('+1day');
     $rrule->clearCache();
     $this->assertEquals(date_create('2007-01-01'), $rrule[0], 'No modification possible of dtstart');
     $rrule = new RRule(array('freq' => 'daily', 'count' => 10, 'dtstart' => '2007-01-01'));
     // offsetGet
     $this->assertEquals(date_create('2007-01-01'), $rrule[0]);
     $rrule[0]->modify('+1 day');
     $this->assertEquals(date_create('2007-01-01'), $rrule[0], 'No modification possible with offsetGet (uncached)');
     $rrule[0]->modify('+1 day');
     $this->assertEquals(date_create('2007-01-01'), $rrule[0], 'No modification possible with offsetGet (cached)');
     // iterate
     $rrule->clearCache();
     foreach ($rrule as $occurrence) {
         break;
     }
     $this->assertEquals(date_create('2007-01-01'), $occurrence);
     $occurrence->modify('+1 day');
     $this->assertEquals(date_create('2007-01-01'), $rrule[0], 'No modification possible with foreach (uncached)');
     foreach ($rrule as $occurrence) {
         break;
     }
     $this->assertEquals(date_create('2007-01-01'), $occurrence);
     $occurrence->modify('+1 day');
     $this->assertEquals(date_create('2007-01-01'), $rrule[0], 'No modification possible with foreach (cached)');
     // getOccurences
     $occurrences = $rrule->getOccurrences();
     $this->assertEquals(date_create('2007-01-01'), $occurrences[0]);
     $occurrences[0]->modify('+1 day');
     $this->assertEquals(date_create('2007-01-02'), $occurrences[0]);
     $this->assertEquals(date_create('2007-01-01'), $rrule[0], 'No modification possible with getOccurences (uncached version)');
     $occurrences = $rrule->getOccurrences();
     $this->assertEquals(date_create('2007-01-01'), $occurrences[0]);
     $occurrences[0]->modify('+1 day');
     $this->assertEquals(date_create('2007-01-02'), $occurrences[0]);
     $this->assertEquals(date_create('2007-01-01'), $rrule[0], 'No modification possible with getOccurences (cached version)');
     // getOccurrencesBetween
     $occurrences = $rrule->getOccurrencesBetween(null, null);
     $this->assertEquals(date_create('2007-01-01'), $occurrences[0]);
     $occurrences[0]->modify('+1 day');
     $this->assertEquals(date_create('2007-01-02'), $occurrences[0]);
     $this->assertEquals(date_create('2007-01-01'), $rrule[0], 'No modification possible with getOccurences (uncached version)');
     $occurrences = $rrule->getOccurrencesBetween(null, null);
     $this->assertEquals(date_create('2007-01-01'), $occurrences[0]);
     $occurrences[0]->modify('+1 day');
     $this->assertEquals(date_create('2007-01-02'), $occurrences[0]);
     $this->assertEquals(date_create('2007-01-01'), $rrule[0], 'No modification possible with getOccurences (cached version)');
 }