Skip to content

RRuleInterface

Rémi Lanvin edited this page Oct 2, 2019 · 12 revisions

Any RRule or RSet object will implement this interface and have the following methods.

Methods

isFinite() and isInfinite()

(version >= 1.1)

Indicate whether or not the rule has an end (count or until), or is infinite. Return true or false.

Infinite rules cannot be counted, and you cannot get all occurrences as a array.

Example: Every day, forever

$rrule = new RRule([
    'FREQ' => 'DAILY',
    'INTERVAL' => 1,
    // no COUNT or UNTIL
]);
$rrule->isFinite(); // false
$rrule->isInfinite(); // true

count()

Returns the number of occurrences, for finite rule only. This method will throw a LogicException if the rule has not COUNT or UNTIL part, as there is an infinity of occurrences.

A RRule object implements the Countable interface, meaning that you can call count($rrule) or $rrule->count().

Not to be confused with the COUNT rule part.

Example: Every day in 2016

$rrule = new RRule([
    'FREQ' => 'DAILY',
    'DTSTART' => '2016-01-01',
    'UNTIL' => '2016-12-31'
]);

count($rrule); // 366, it's a leap year!

getOccurrences($limit)

$limit available in version >= 1.5

Returns an array of occurrences (as DateTime). When $limit is 0 (default), it will return all occurrences. This method will throw a LogicException if the rule has not COUNT or UNTIL part, as there is an infinity of occurrences.

Note: you do not need this method if you just want to traverse the rule, you can use the object directly in a foreach.

Example: Every day in 2016

$rrule = new RRule([
    'FREQ' => 'DAILY',
    'DTSTART' => '2016-01-01',
    'UNTIL' => '2016-12-31'
]);

$result = $rrule->getOccurrences(); // an array of DateTime objects

getOccurrencesBetween($begin, $end, $limit)

$limit available in version >= 1.5

Returns an array of occurrences between $begin and $end (included). When $limit is 0 (default), it will return all occurrences. Both dates can be given as a string understandable by PHP's DateTime constructor (for example 2015-07-01 14:46:30) a UNIX Timestamp (as int) or a DateTime object, or null.

  • If $begin is null this method will return all occurrences before $end.
  • If $end is null this method will return all occurrences after $begin. If the rule is infinite (no COUNT or UNTIL) it will throw a LogicException.
  • If both are null this method is the same as getOccurrences()

Note: If you are working with events, remember that there is no notion of duration. In other words, this method will returns the events that start between $begin and $end, but not the events that started before $begin and might still be ongoing due to their duration. If you want the later behavior, you should first subtract duration from $begin, to get the occurrences between "begin - duration" and "end".

Example: Every first Monday of the month in 2016

$rrule = new RRule([
    'FREQ' => 'MONTHLY',
    'BYDAY' => '1MO',
    'DTSTART' => '2016-01-01',
    'UNTIL' => '2016-12-31'
]);

$result = $rrule->getOccurrencesBetween('2016-01-01', '2016-01-31');
print_r($result);

// only the first Monday between 1 January and 31 January
// Array
// (
//     [0] => DateTime Object
//         (
//             [date] => 2016-01-04 00:00:00.000000
//             [timezone_type] => 3
//             [timezone] => Europe/Helsinki
//         )
// )

getOccurrencesBefore($date, $inclusive = false, $limit = null)

(version >= 2.0)

Return all the occurrences before a date. $inclusive indicates whether or not to include $date if date is an occurrence. $limit limits the amound of occurrences (0, null or false for everything).

getOccurrencesAfter($date, $inclusive = false, $limit = null)

(version >= 2.0)

Return all the occurrences after a date. $inclusive indicates whether or not to include $date if date is an occurrence. $limit limits the amound of occurrences (0, null or false for everything).

getNthOccurrenceBefore($date, $index)

(version >= 2.0)

Return the Nth occurrence before a date (non inclusive).

getNthOccurrenceAfter($date, $index)

(version >= 2.0)

Return the Nth occurrence after a date (non inclusive).

getNthOccurrenceFrom($date, $index)

(version >= 2.0)

Return the Nth occurrence before (if $index is negative) or after (if $index is positive) a date (non inclusive).

occursAt($datetime)

Returns true if $datetime is an occurrence of the rule, or false otherwise. $datetime can be a string understandable by PHP's DateTime constructor (for example 2015-07-01 14:46:30) a UNIX Timestamp (as int) or a DateTime object.

Note: If you are working with events, remember that there is no notion of duration. In other words, this method will tell you if a event starts as $datetime, but not if an event is still ongoing at $datetime. If you want the later behavior, you should call getOccurrencesBetween() with "datetime - duration" and "datetime".

Example: Every other week

$rrule = new RRule([
    'FREQ' => 'WEEKLY',
    'INTERVAL' => 2,
    'DTSTART' => '2016-01-01',
]);

$rrule->occursAt('2016-01-01'); // true
$rrule->occursAt('2016-01-07'); // false

Note

The RRule interface extends the following interfaces: IteratorAggregate, ArrayAccess and Countable.