Download the PHP package ckuran/period without Composer
On this page you can find all versions of the php package ckuran/period. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ckuran/period
More information about ckuran/period
Files in ckuran/period
Package period
Short Description Complex period comparisons
License MIT
Homepage https://github.com/spatie/period
Informations about the package period
Complex period comparisons
This package adds support for comparing multiple dates with each other. You can calculate the overlaps and differences between n-amount of periods, as well as some more basic comparisons between two periods.
Periods can be constructed from any type of DateTime
implementation,
making this package compatible with custom DateTime
implementations like
Carbon
(see cmixin/enhanced-period to
convert directly from and to CarbonPeriod).
Periods are always immutable, there's never the worry about your input dates being changed.
This package is still a work in progress.
Installation
You can install the package via composer:
Usage
Quick reference
Creating a period
The static ::make
constructor can also take strings and other implementations of DateTimeInterface
,
as well as an extra format string, in case the textual dates passed aren't of the format Y-m-d
or Y-m-d H:i:s
.
Length and boundaries
Comparisons
Comparing periods
Overlaps with any other period:
this method returns a PeriodCollection
multiple Period
objects representing the overlaps.
Overlap with all periods: this method only returns one period where all periods overlap.
Diff between multiple periods:
this method returns a PeriodCollection
multiple Period
objects
representing the diffs between several periods and one.
Overlaps with: this method returns a boolean indicating of two periods overlap or not.
Touches with: this method determines if two periods touch each other.
Gap: returns the gap between two periods.
If no gap exists, null
is returned.
Boundaries of a collection: get one period representing the boundaries of a collection.
Gaps of a collection: get all the gaps of a collection.
Overlap multiple collections: returns the overlap between collections. This means an AND operation between collections, and an OR operation within the same collection.
Working with PeriodCollection
Period collections are constructed from several periods:
They may be looped over directly and its contents will be recognised by your IDE:
You may destruct them:
And finally construct one collection from another:
Precision
Date precision is of utmost importance if you want to reliably compare two periods. The the following example:
Given two periods:
[2018-01-01, 2018-01-15]
and[2018-01-15, 2018-01-31]
; do they overlap?
At first glance the answer is "yes": they overlap on 2018-01-15
.
But what if the first period ends at 2018-01-15 10:00:00
,
while the second starts at 2018-01-15 15:00:00
?
Now they don't anymore!
This is why this package requires you to specify a precision with each period. Only periods with the same precision can be compared.
A more in-depth explanation on why precision is so important can be found here. A period's precision can be specified when constructing that period:
The default precision is set on days. These are the available precision options:
Boundaries
By default, period comparisons are done with included boundaries. This means that these two periods overlap:
The length of a period will also include both boundaries:
It's possible to override the boundary behaviour:
There are four types of boundary exclusion:
Compatibility
You can construct a Period
from any type of DateTime
object such as Carbon:
Note that as soon as a period is constructed, all further operations on it are immutable. There's never the danger of changing the input dates.
You can iterate a Period
like a regular DatePeriod
with the precision specified on creation:
Visualizing periods
You can visualize one or more Period
objects as well as PeriodCollection
objects to see how they related to one another:
And visualize will return the following string:
The visualizer has a configurable width provided upon creation which will control the bounds of the displayed periods:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
We publish all received postcards on our company website.
Credits
- Brent Roose
- All Contributors
Support us
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
License
The MIT License (MIT). Please see License File for more information.