Coder Social home page Coder Social logo

recurr's Introduction

Recurr Build Status Latest Stable Version Total Downloads Latest Unstable Version License

Recurr is a PHP library for working with recurrence rules (RRULE) and converting them in to DateTime objects.

Recurr was developed as a precursor for a calendar with recurring events, and is heavily inspired by rrule.js.

Installation

Recurr is hosted on packagist, meaning you can install it with Composer.

  1. Create a composer.json file

    {
        "require": {
            "simshaun/recurr": "dev-master"
        }
    }

    We recommend using a stable version instead of dev-master.

  2. Install Composer and run the install command

    wget http://getcomposer.org/composer.phar
    php composer.phar install
  3. Include Composer's autoloader

    require 'vendor/autoload.php';

RRULE to DateTime objects

$timezone    = 'America/New_York';
$startDate   = new \DateTime('2013-06-12 20:00:00', new \DateTimeZone($timezone));
$endDate     = new \DateTime('2013-06-14 20:00:00', new \DateTimeZone($timezone)); // Optional
$rule        = new \Recurr\Rule('FREQ=MONTHLY;COUNT=5', $startDate, $endDate, $timezone);
$transformer = new \Recurr\Transformer\ArrayTransformer();

print_r($transformer->transform($rule));
  1. $transformer->transform(...) returns a RecurrenceCollection of Recurrence objects.
  2. Each Recurrence has getStart() and getEnd() methods that return a \DateTime object.
  3. If the transformed Rule lacks an end date, getEnd() will return a \DateTime object equal to that of getStart().

Note: The transformer has a "virtual" limit (default 732) on the number of objects it generates. This prevents the script from crashing on an infinitely recurring rule. You can change the virtual limit in the call to transform or the ArrayTransformer constructor.

Transformation Constraints

Constraints (\Recurr\Transformer\ConstraintInterface) are used by the ArrayTransformer to allow or prevent certain dates from being added to a RecurrenceCollection. Recurr provides the following constraints:

  • AfterConstraint(\DateTime $after, $inc = false)
  • BeforeConstraint(\DateTime $before, $inc = false)
  • BetweenConstraint(\DateTime $after, \DateTime $before, $inc = false)

$inc defines what happens if $after or $before are themselves recurrences. If $inc = true, they will be included in the collection. For example,

$startDate   = new \DateTime('2014-06-17 04:00:00');
$rule        = new \Recurr\Rule('FREQ=MONTHLY;COUNT=5', $startDate);
$transformer = new \Recurr\Transformer\ArrayTransformer();

$constraint = new \Recurr\Transformer\Constraint\BeforeConstraint(new \DateTime('2014-08-01 00:00:00'));
print_r($transformer->transform($rule, null, $constraint));

Note: If building your own constraint, it is important to know that dates which do not meet the constraint's requirements do not count toward the transformer's virtual limit. If you manually set your constraint's $stopsTransformer property to false, the transformer might crash via an infinite loop. See the BetweenConstraint for an example on how to prevent that.

Post-Transformation RecurrenceCollection Filters

RecurrenceCollection provides the following chainable helper methods to filter out recurrences:

  • startsBetween(\DateTime $after, \DateTime $before, $inc = false)
  • startsBefore(\DateTime $before, $inc = false)
  • startsAfter(\DateTime $after, $inc = false)
  • endsBetween(\DateTime $after, \DateTime $before, $inc = false)
  • endsBefore(\DateTime $before, $inc = false)
  • endsAfter(\DateTime $after, $inc = false)

$inc defines what happens if $after or $before are themselves recurrences. If $inc = true, they will be included in the filtered collection. For example,

pseudo...
2014-06-01 startsBetween(2014-06-01, 2014-06-20) // false
2014-06-01 startsBetween(2014-06-01, 2014-06-20, true) // true

Note: RecurrenceCollection extends the Doctrine project's ArrayCollection class.

RRULE to Text

Recurr supports transforming some recurrence rules in to human readable text. This feature is still in beta and only supports yearly, monthly, weekly, and daily frequencies. It is not yet localized and only supports English.

$rule = new Rule('FREQ=YEARLY;INTERVAL=2;COUNT=3;', new \DateTime());

$textTransformer = new TextTransformer();
echo $textTransformer->transform($rule);

Warnings

  • Monthly recurring rules: If your start date is on the 29th, 30th, or 31st, Recurr will skip the months that have less than that number of days. This behavior is configurable:
$timezone    = 'America/New_York';
$startDate   = new \DateTime('2013-01-31 20:00:00', new \DateTimeZone($timezone));
$rule        = new \Recurr\Rule('FREQ=MONTHLY;COUNT=5', $startDate, null, $timezone);
$transformer = new \Recurr\Transformer\ArrayTransformer();

$transformerConfig = new \Recurr\Transformer\ArrayTransformerConfig();
$transformerConfig->enableLastDayOfMonthFix();
$transformer->setConfig($transformerConfig);

print_r($transformer->transform($rule));

/* Recurrences:
 * 2013-01-31
 * 2013-02-28
 * 2013-03-31
 * 2013-04-30
 * 2013-05-31
 */

Contribute

Recurr is still in beta, and is most likely not 100% free of bugs. Feel free to comment or make pull requests. Please include tests with PRs.

License

Recurr is licensed under the MIT License. See the LICENSE file for details.

recurr's People

Contributors

avr avatar jarofgreen avatar pborreli avatar simshaun avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.