RRule\RRule::getOccurrences PHP Method

getOccurrences() public method

Return all the occurrences in an array of \DateTime.
public getOccurrences ( ) : array
return array An array of \DateTime objects
    public function getOccurrences()
    {
        if ($this->isInfinite()) {
            throw new \LogicException('Cannot get all occurrences of an infinite recurrence rule.');
        }
        // cached version already computed
        if ($this->total !== null) {
            $res = array();
            foreach ($this->cache as $occurrence) {
                $res[] = clone $occurrence;
                // we have to clone because DateTime is not immutable
            }
            return $res;
        }
        $res = array();
        foreach ($this as $occurrence) {
            $res[] = $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)');
 }
All Usage Examples Of RRule\RRule::getOccurrences