fyre/period

A Date Period library.

v3.0.3 2024-06-29 02:14 UTC

This package is auto-updated.

Last update: 2024-08-29 02:31:44 UTC


README

FyrePeriod is a free, open-source date period library for PHP.

Table Of Contents

Installation

Using Composer

composer require fyre/period

In PHP:

use Fyre\Period\Period;
use Fyre\Period\PeriodCollection;

Periods

  • $start is a DateTime or string representing the start date.
  • $end is a DateTime or string representing the end date.
  • $granularity is a string representing the granularity, and can be one of either "year", "month", "day", "hour", "minute" or "second", and will default to "day".
  • $excludeBoundaries is a string representing the excluded boundaries, and can be one of either "none", "start", "end" or "both", and will default to "none".
$period = new Period($start, $end, $granularity, $excludeBoundaries);

The Period is an implementation of an Iterator and can be used in a foreach loop.

foreach ($period AS $date) { }

Contains

Determine if this period contains another Period.

  • $other is the Period to compare against.
$contains = $period->contains($other);

Diff Symmetric

Get the symmetric difference between the periods.

  • $other is the Period to compare against.
$diffSymmetric = $period->diffSymmetric($other);

This method will return a new PeriodCollection.

End

Get the end date.

$end = $period->end();

This method will return a DateTime.

End Equals

Determine if this period ends on a given date.

  • $date is the DateTime to compare against.
$endEquals = $period->endEquals($date);

Ends After

Determine if this period ends after a given date.

  • $date is the DateTime to compare against.
$endsAfter = $period->endsAfter($date);

Ends After Or Equals

Determine if this period ends on or after a given date.

  • $date is the DateTime to compare against.
$endsAfterOrEquals = $period->endsAfterOrEquals($date);

Ends Before

Determine if this period ends before a given date.

  • $date is the DateTime to compare against.
$endsBefore = $period->endsBefore($date);

Ends Before Or Equals

Determine if this period ends on or before a given date.

  • $date is the DateTime to compare against.
$endsBeforeOrEquals = $period->endsBeforeOrEquals($date);

Equals

Determine if this period equals another Period.

  • $other is the Period to compare against.
$equals = $period->equals($other);

Gap

Get the gap between the periods.

  • $other is the Period to compare against.
$gap = $period->gap($other);

This method will return a new Period, or null if there's no gap.

Granularity

Get the granularity.

$granularity = $period->granularity();

Included End

Get the included end date.

$includedEnd = $period->includedEnd();

This method will return a DateTime.

Included Start

Get the included start date.

$includedStart = $period->includedStart();

This method will return a DateTime.

Includes End

Determine if the Period includes the end date.

$includesEnd = $period->includesEnd();

Includes

Determine if this period includes a given date.

  • $date is the DateTime to compare against.
$includes = $period->includes($date);

Includes Start

Determine if the Period includes the start date.

$includesStart = $period->includesStart();

Length

Get the length of the period.

$length = $period->length();

Overlap

Get the overlap of the periods.

  • $other is the Period to compare against.
$overlap = $period->overlap($other);

This method will return a new Period, or null if there's no overlap.

Overlap All

Get the overlap of all the periods.

$overlapAll = $period->overlapAll(...$others);

This method will return a new Period, or null if there's no overlap.

Overlap Any

Get the overlaps of any of the periods.

$overlapAny = $period->overlapAny(...$others);

This method will return a new PeriodCollection.

Overlaps With

Determine if this period overlaps with another Period.

  • $other is the Period to compare against.
$overlapsWith = $period->overlapsWith($other);

Renew

Create a new period with the same length after this period.

$renewed = $period->renew();

This method will return a new Period.

Start

Get the start date.

$start = $period->start();

This method will return a DateTime.

Start Equals

Determine if this period starts on a given date.

  • $date is the DateTime to compare against.
$startEquals = $period->startEquals($date);

Starts After

Determine if this period starts after a given date.

  • $date is the DateTime to compare against.
$startsAfter = $period->startsAfter($date);

Starts After Or Equals

Determine if this period starts on or after a given date.

  • $date is the DateTime to compare against.
$startsAfterOrEquals = $period->startsAfterOrEquals($date);

Starts Before

Determine if this period starts before a given date.

  • $date is the DateTime to compare against.
$startsBefore = $period->startsBefore($date);

Starts Before Or Equals

Determine if this period starts on or before a given date.

  • $date is the DateTime to compare against.
$startsBeforeOrEquals = $period->startsBeforeOrEquals($date);

Subtract

Get the inverse overlap of the periods.

  • $other is the Period to compare against.
$subtract = $period->subtract($other);

This method will return a new PeriodCollection.

Subtract All

Get the inverse overlap of all periods.

$subtractAll = $period->subtractAll(...$others);

This method will return a new PeriodCollection.

Touches

Determine if this period touches another Period.

  • $other is the Period to compare against.
$touches = $period->touches($other);

Period Collections

All arguments supplied will be used as periods for the collection.

$collection = new PeriodCollection(...$periods);

The PeriodCollection is an implementation of an Iterator and can be used in a foreach loop.

foreach ($collection AS $period) { }

Add

Add periods to the collection.

All arguments supplied will be used as periods to add to the collection.

$added = $collection->add(...$periods);

This method will return a new PeriodCollection.

Boundaries

Get the boundaries of the collection.

$boundaries = $collection->boundaries();

This method will return a new Period, or null if the collection is empty.

Gaps

Get the the gaps between the periods in the collection.

$gaps = $collection->gaps();

This method will return a new PeriodCollection.

Intersect

Intersect a period with every period in the collection.

  • $period is the Period to compare against.
$intersect = $collection->intersect($period);

This method will return a new PeriodCollection.

Overlap All

Get the overlap of all the collections.

All arguments supplied will be used as collections to compare against.

$overlapAll = $collection->overlapAll(...$others);

This method will return a new PeriodCollection.

Sort

Sort the periods.

$sorted = $collection->sort();

This method will return a new PeriodCollection.

Subtract

Get the inverse overlap of the collections.

  • $others is the PeriodCollection to compare against.
$subtract = $collection->subtract($others);

This method will return a new PeriodCollection.

Unique

Filter the periods to remove duplicates.

$unique = $collection->unique();

This method will return a new PeriodCollection.