rflex/period

There is no license information available for the latest version (0.6.5) of this package.

Customized library for comparing periods easily. This is an extension of Carbon\Period.

0.6.5 2023-08-06 01:12 UTC

This package is auto-updated.

Last update: 2024-04-06 02:33:15 UTC


README

Period is a library that extends CarbonPeriod to supply new customized methods.

You can find the CarbonPeriod documentation here.

Installation & Usage

composer require rflex/period

Import the class:

use Rflex\Period;
use Rflex\Event; // Extension of the Carbon class.

Testing

PHPUnit

./vendor/phpunit/phpunit/phpunit

PHPStan

./vendor/bin/phpstan analyse

Available methods

addDay() and addDays($days): void

Add one or a number of days to the period.

$period->addDay(true, false); // Add a day to the start of the period.
$period->addDays(5, true, false); // Add five days to the start of the period.

subDay() and subDays($days): void

Subtract one or a number of days to the period.

$period->subDay(false, true); // Subtract a day to the end of the period.
$period->subDays(3, false, true); // Subtract three days to the end of the period.

getSeconds(): int

Returns the total number of seconds of the period.

$period->getSeconds();

getMinutes(): int

Returns the total number of minutes of the period.

$period->getMinutes();

getHours(): int

Returns the total number of hours of the period.

$period->getHours();

union($period, $intersects): Period

Returns a new Period that is the sum of another two Periods. By default it will sum two periods without validating if they intersect. With the $intersects in true you can specify that you only need to sum two periods that intersect with each other.

$period = Period::between(Carbon::now(), Carbon::now()->addDay());
$period2 = Period::between(carbon::now(), Carbon::now()->addDays(2));
$unifiedPeriod = $period->union($period2); // Returns a new unified Period.

intersection($period): Period

Get the shared period between two other periods.

$period = Period::between(Carbon::now(), Carbon::now()->addDay());
$period2 = Period::between(carbon::now(), Carbon::now()->addDays(2));
$intersection = $period->intersection($period2); // Returns a new intersected Period.

intersects($period): bool

Checks if a period intersects with another.

$period = Period::between(Carbon::now(), Carbon::now()->addDay());
$period2 = Period::between(carbon::now(), Carbon::now()->addDays(2));
$period->intersects($period2); // Returns true.

difference($period): array|null

Returns the difference between two Periods. This function can return more than one Period if the original Period was cut in different pieces. [totalLengthInSeconds, 'periods' => []]

$period = Period::between(Carbon::now(), Carbon::now()->addDay());
$period2 = Period::between(carbon::now(), Carbon::now()->addDays(2));
$unifiedPeriod = $period->difference($period2); // Returns an array.

differenceWithEvent($event, $point): int

Returns the difference in seconds between a Period and an Event. The comparison is between the Event and a point of the Period. 0 = start, 1 = end.

$period = Period::between(Carbon::now(), Carbon::now()->addDay());
$event = Event::create(carbon::now());
$seconds = $period->differenceWithEvent($event); // Returns the difference between the two in seconds.

setLengthInSeconds($seconds): void

Set the length of the period in seconds from the start.

$period->setLengthInSeconds(3600);

setLengthInMinutes($minutes): void

Set the length of the period in minutes from the start.

$period->setLengthInMinutes(700);

setLengthInHours($hours): void

Set the length of the period in hours from the start.

$period->setLengthInHours(48);