1. Go to this page and download the library: Download jrbarnard/recurrence library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jrbarnard / recurrence example snippets
$occurrences = new Occurrences();
// Add an occurrence in
$occurrences->push(new DateTime);
// $popped will be the last added occurrence
$popped = $occurrences->pop();
$occurrences->push(new DateTime);
// Can get an occurrence by index
$occurrence = $occurrences->getOccurrence(0);
$occurrences = new Occurrences();
// Same as push
$occurrences[] = new DateTime();
// $occurrence will be the 0th index DateTime object
$occurrence = $occurrences[0];
use JRBarnard\Reccurrence\Intervals\IntervalInterface;
class ExampleInterval implements IntervalInterface
{
/**
* Method that finds the next occurrence of the interval from current
*
* @param DateTime $current
* @param $direction
*
* @return DateTime
*/
public function findNextOccurrence(DateTime $current, $direction = self::FORWARDS)
{
$interval = new \DateInterval('PT12M');
$cloned = clone $current;
if ($direction === self::BACKWARDS) {
return $cloned->sub($interval);
} else {
return $cloned->add($interval);
}
}
}
new DailyInterval(12);
(new DailyInterval())->setNumberOfDays(12);
new HourlyInterval(1.5);
(new HourlyInterval())->setNumberOfHours(1.5);
// Basic usage via constructor
new WeeklyInterval([WeeklyInterval::WEDNESDAY, WeeklyInterval::TUESDAY], 3);
// Standard setters
$interval = new WeeklyInterval();
$interval->setDays([WeeklyInterval::TUESDAY, WeeklyInterval::WEDNESDAY]);
$interval->setWeeks(3);
// Magic setters
(new WeeklyInterval())->everyTuesday()->andEveryWednesday()->ofEvery3rdWeek();
// Basic usage via constructor
new MonthlyInterval(MonthlyInterval::LAST, [MonthlyInterval::WEDNESDAY, MonthlyInterval::THURSDAY], 2);
// Standard setters
$interval = new MonthlyInterval();
$interval->setDays([MonthlyInterval::WEDNESDAY, MonthlyInterval::THURSDAY]);
$interval->setFrequency(MonthlyInterval::LAST);
$interval->setMonths(2);
// Magic setters
(new MonthlyInterval())->every(MonthlyInterval::LAST, [MonthlyInterval::WEDNESDAY, MonthlyInterval::THURSDAY])->ofEveryMonth(2);
(new MonthlyInterval())->everyLast()->wednesday()->andThursday()->ofEvery2ndMonth();
$start = new DateTime(); // Now
$end = (new DateTime())->add(new DateInterval('P1W'));
$interval = new HourlyInterval(1.5);
$iterator = new DateIntervalIterator($start, $interval, $end);
foreach($iterator as $occurrence) {
// $occurrence will be a DateTime instance of every hour and half till next week
}
$start = new DateTime(); // Now
$end = 50;
$interval = new WeeklyInterval([WeeklyInterval::WEDNESDAY, WeeklyInterval::THURSDAY], 2);
$iterator = new DateIntervalIterator($start, $interval, $end);
foreach($iterator as $occurrence) {
// $occurrence will be a DateTime instance of every 2nd Wednesday and Thursday up to 50 occurrences.
}
$iterator->count();
// Or:
count($iterator);
$iterator->skip(new DateTime('2012-02-02 12:30:00'));
// You can also check if the iterator should skip a particular datetime:
$iterator->shouldSkip('2012-02-02 12:30:00');
// Will return true
$iterator->setEndAfter(10);
$iterator->setEndAfter('2016-12-12 17:30:00');
// You can also use a getter:
$iterator->getEndAfter();
// It defaults to 100 when you instantiate the iterator
$iterator->setMaxOccurrences(20);
// If you now try and set an end after over the max it will throw an exception
// If you set an end after to a date, it wil; stop iterating either when it reaches that date or the max occurrences,
// whichever occurs first.