Download the PHP package spatie/period without Composer

On this page you can find all versions of the php package spatie/period. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package period

Complex period comparisons

Latest Version on Packagist Quality Score Total Downloads

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.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

Usage

Creating periods

You're encouraged to create periods using their static constructor:

You can manually construct a period, but you'll need to manually provide its precision and boundaries. Using Period::make, the default precision (Precision::DAY()) and default boundaries (Boundaries::EXCLUDE_NONE()) are used.

Before discussing the API provided by this package, it's important to understand both how precision and boundaries are used.

Precision

Date precision is of utmost importance if you want to reliably compare two periods. The following example:

Given two periods: [2021-01-01, 2021-01-15] and [2021-01-15, 2021-01-31]; do they overlap?

At first glance the answer is "yes": they overlap on 2021-01-15. But what if the first period ends at 2021-01-15 10:00:00, while the second starts at 2021-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:

Reference

The Period class offers a rich API to interact and compare with other periods and collections of periods. Take into account that only periods with the same precision can be compared:


On top of comparisons, the Period class also offers a bunch of operations:

overlap(Period ...$others): ?static

Overlaps two or more periods on each other. The resulting period will be the union of all other periods combined.

overlapAny(Period ...$others): PeriodCollection

Overlaps two or more periods on each other. Whenever two or more periods overlap, that overlapping period is added to a collection which will be returned as the final result.

subtract(Period ...$others): PeriodCollection

Subtracts one or more periods from the main period. This is the inverse operation of overlap.

gap(Period $period): ?static

Gets the gap between two periods, or 0 if the periods overlap.

diffSymmetric(Period $other): PeriodCollection

Performs a symmetric diff between two periods.

renew(): static

Renew the current period, creating a new period with the same length that happens after the current period.


Next, the Period class also has some getters:


The PeriodCollection class represents a collection of periods and has some useful methods on its own:

overlapAll(PeriodCollection ...$others): PeriodCollection

Overlaps all collection periods on each other.

subtract(PeriodCollection|Period ...$others): PeriodCollection

Subtracts a period or a collection of periods from a period collection.

boundaries(): ?Period

Creates a new period representing the outer boundaries of the collection.

gaps(): static

Gives the gaps for all periods within this collection.

intersect(Period $intersection): static

Intersects given period with every period within a collection. The result is a new collection of overlapping periods between given period and every period in the collection. When there's no overlap, the original period is discarded.

union(): static

Merges all periods in collection with overlapping ranges.


Finally, there are a few utility methods available on PeriodCollection as well:

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've found a bug regarding security please mail [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, Kruikstraat 22, 2021 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of period with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package spatie/period contains the following files

Loading the files please wait ....