RRule\RSet::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 set.');
        }
        // 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

Ejemplo n.º 1
0
 public function testRSetInRset()
 {
     $rset = new RSet();
     $rset->addRRule($rset);
     $rset->addDate('2016-03-21');
     $this->assertEquals(array(date_create('2016-03-21')), $rset->getOccurrences(), 'Adding the RSet into itself does not explode');
     $sub_rset = new RSet();
     $sub_rset->addDate('2016-03-21 10:00');
     $sub_rset->addDate('2016-03-21 11:00');
     $rset = new RSet();
     $rset->addRRule($sub_rset);
     $this->assertEquals(array(date_create('2016-03-21 10:00'), date_create('2016-03-21 11:00')), $rset->getOccurrences());
     $rset->addExDate('2016-03-21 11:00');
     $this->assertEquals(array(date_create('2016-03-21 10:00')), $rset->getOccurrences());
 }