PHP code example of cmixin / enhanced-period

1. Go to this page and download the library: Download cmixin/enhanced-period library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cmixin / enhanced-period example snippets




use Carbon\CarbonPeriod;
use Cmixin\EnhancedPeriod;

CarbonPeriod::mixin(EnhancedPeriod::class); // This line is not needed if you use Laravel default auto-discovery.

// Use toEnhancedPeriod to convert `Carbon\CarbonPeriod` objects to `Spatie\Period\Period` ones
$a = CarbonPeriod::create('2018-01-01', '2018-01-10')->toEnhancedPeriod();
$b = CarbonPeriod::create('2018-01-15', '2018-01-31')->toEnhancedPeriod();

// Use fromEnhancedPeriod to convert `Spatie\Period\Period` objects to `Carbon\CarbonPeriod` ones
echo CarbonPeriod::fromEnhancedPeriod($a->gap($b));

// Or you can directly call gap() or most of the other `Spatie\Period\Period` methods directly on `Carbon\CarbonPeriod`:
$a = CarbonPeriod::create('2018-01-01', '2018-01-10');
$b = CarbonPeriod::create('2018-01-15', '2018-01-31');

// It will use `Spatie\Period\Period::gap` and automatically convert the result to `Carbon\CarbonPeriod`
echo $a->gap($b);


length(): int

CarbonPeriod::create('2019-08-20', '2019-09-01')->length();

overlapsWith($period, ...$arguments): bool

CarbonPeriod::create('2019-08-20', '2019-09-01')->overlapsWith('2019-08-28', '2019-09-03');

touchesWith($period, ...$arguments): bool

CarbonPeriod::create('2019-08-20', '2019-09-01')->touchesWith('2019-09-02', '2019-09-06');

duration(): Spatie\Period\PeriodDuration

CarbonPeriod::create('2019-08-20', '2019-09-01')->duration();

overlap($period, ...$arguments): ?CarbonPeriod

CarbonPeriod::create('2019-08-20', '2019-09-01')->overlap('2019-08-25', '2019-09-05'); // [2019-08-25, 2019-09-01]

overlapAny($period, ...$arguments): CarbonPeriod[]

$a = CarbonPeriod::create('2018-01-01', '2018-01-31');
$b = CarbonPeriod::create('2018-02-10', '2018-02-20');
$c = CarbonPeriod::create('2018-03-01', '2018-03-31');
$d = CarbonPeriod::create('2018-01-20', '2018-03-10');

foreach ($d->overlapAny($a, $b, $c) as $period) {
  echo $period."\n";
}

overlapAll(...$periods): ?CarbonPeriod

$a = CarbonPeriod::create('2018-01-01', '2018-01-31');
$b = CarbonPeriod::create('2018-01-10', '2018-01-15');
$c = CarbonPeriod::create('2018-01-10', '2018-01-31');

echo $a->overlapAll($b, $c);

diffAny($period, ...$arguments): CarbonPeriod[]

$a = CarbonPeriod::create('2018-01-01', '2018-01-15');
$b = CarbonPeriod::create('2018-01-10', '2018-01-30');

foreach ($a->diffAny($b) as $period) {
  echo $period."\n";
}

diff(...$periods): CarbonPeriod[]

$a = CarbonPeriod::create('2018-01-01', '2018-01-31');
$b = CarbonPeriod::create('2018-02-10', '2018-02-20');
$c = CarbonPeriod::create('2018-02-11', '2018-03-31');

$current = CarbonPeriod::create('2018-01-20', '2018-03-15');

foreach ($current->diff($a, $b, $c) as $period) {
  echo $period."\n";
}

gap($period, ...$arguments): ?CarbonPeriod

$a = CarbonPeriod::create('2018-01-01', '2018-01-10');
$b = CarbonPeriod::create('2018-01-15', '2018-01-31');

echo $a->gap($b);

fromEnhancedPeriod(Period $period, bool $mutable = false): CarbonPeriod

$period = CarbonPeriod::fromEnhancedPeriod(Period::make('2018-01-01', '2018-01-10'));

fromNullableEnhancedPeriod(Period|null $period, bool $mutable = false): CarbonPeriod|null

$period = CarbonPeriod::fromNullableEnhancedPeriod($spatiePeriod);

fromPeriodCollection(PeriodCollection $periods, $mutable = false): CarbonPeriod[]

$periods = CarbonPeriod::fromPeriodCollection(new PeriodCollection(
  Period::make('2018-01-01', '2018-01-10'),
  Period::make('2018-01-15', '2018-01-31')
));

foreach ($periods as $period) {
  echo $period; // $period is a CarbonPeriod instance
}

convertDateIntervalToPrecision(DateInterval $interval): int|Precision

$precision = CarbonPeriod::convertDateIntervalToPrecision(CarbonInterval::day());
// Precision::DAY() (with PHP >= 8 and spatie/period >= 2)
// Precision::DAY (in older versions)

convertPrecisionMaskToDateInterval(int|Precision $precisionMask): CarbonInterval

$interval = CarbonPeriod::convertPrecisionMaskToDateInterval(Precision::DAY()); // CarbonInterval::day()
// Pass Precision object (such as Precision::DAY()) with PHP >= 8 and spatie/period >= 2
// Pass integer masks (such as Precision::DAY) with older versions