cmixin / enhanced-period
将 `Carbon\CarbonPeriod` 转换为 `Spatie\Period\Period` 及反之的 Carbon 混合组件
Requires
- php: ^7.2 || ^8.0
- nesbot/carbon: ^2.23.0-beta.1 || ^3.0@dev || ^4.0@dev
- spatie/period: ^1.4.3 || ^2.0.0
Requires (Dev)
- phpunit/phpunit: ^8.5.14 || ^9.3.8
This package is auto-updated.
Last update: 2024-09-12 22:02:43 UTC
README
Carbon mixin 用于将 Carbon\CarbonPeriod
转换为 Spatie\Period\Period
以及反之。
安装
composer require cmixin/enhanced-period
使用方法
<?php 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);
输出(使用默认的 Carbon\CarbonPeriod
字符串转换)
Every 1 day from 2018-01-11 to 2018-01-14
查看你可以在 Spatie\Period\Period
对象上调用的所有方法。
以下是在 CarbonPeriod
实例上可以直接调用的方法
length
length(): int
CarbonPeriod::create('2019-08-20', '2019-09-01')->length();
overlapsWith
overlapsWith($period, ...$arguments): bool
CarbonPeriod::create('2019-08-20', '2019-09-01')->overlapsWith('2019-08-28', '2019-09-03');
你可以传递给 overlapsWith
: Spatie\Period\Period
,CarbonPeriod
,DatePeriod
或构建 CarbonPeriod
的参数。
注意:`->overlapsWith` 的结果将与 `->overlaps` 不同,因为它使用内部 Spatie\Period\Period
和其精度掩码(使用向下取整)。
touchesWith
touchesWith($period, ...$arguments): bool
CarbonPeriod::create('2019-08-20', '2019-09-01')->touchesWith('2019-09-02', '2019-09-06');
你可以传递给 overlapsWith
: Spatie\Period\Period
,CarbonPeriod
,DatePeriod
或构建 CarbonPeriod
的参数。
注意:`->touchesWith` 的结果将与 `->isConsecutiveWith` 不同,因为它使用内部 Spatie\Period\Period
和其精度掩码(使用向下取整)。
duration
警告:此方法需要 spatie/period >= 2.0
duration(): Spatie\Period\PeriodDuration
CarbonPeriod::create('2019-08-20', '2019-09-01')->duration();
获取迭代持续时间的表示。
overlap
警告:由于 即将重命名,目前它在 spatie/period 中称为 overlapSingle
。
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
警告:由于 即将重命名,目前它在 spatie/period 中称为 overlap
。
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"; }
返回至少存在于两个周期中的重叠块。
A [========]
B [==]
C [=====]
CURRENT [===============]
OVERLAP [=] [==] [=]
overlapAll
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);
返回所有周期的合并重叠。
A [============]
B [==]
C [=======]
OVERLAP [==]
diffAny
警告:由于 即将重命名,目前它在 spatie/period 中称为 diffSingle
。
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
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"; }
返回当前周期中未被传递的参数覆盖的周期。
A [====]
B [========]
C [=====]
CURRENT [========================]
DIFF [=] [====]
gap
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);
返回当前周期与传递的参数之间的间隔周期。
A [========]
B [===========]
GAP [==]
fromEnhancedPeriod
fromEnhancedPeriod(Period $period, bool $mutable = false): CarbonPeriod
$period = CarbonPeriod::fromEnhancedPeriod(Period::make('2018-01-01', '2018-01-10'));
将 Spatie 周期转换为 Carbon 周期,你可以将 true
作为第二个参数传递以选择 Carbon
作为日期类而不是默认的 CarbonImmutable
。
fromNullableEnhancedPeriod
fromNullableEnhancedPeriod(Period|null $period, bool $mutable = false): CarbonPeriod|null
$period = CarbonPeriod::fromNullableEnhancedPeriod($spatiePeriod);
与 fromEnhancedPeriod
相同,但允许空值。
fromPeriodCollection
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 }
将 PeriodCollection
对象转换为 CarbonPeriod
实例数组。
你可以将 true
作为第二个参数传递以选择 Carbon
作为日期类而不是默认的 CarbonImmutable
。
convertDateIntervalToPrecision
convertDateIntervalToPrecision(DateInterval $interval): int|Precision
$precision = CarbonPeriod::convertDateIntervalToPrecision(CarbonInterval::day()); // Precision::DAY() (with PHP >= 8 and spatie/period >= 2) // Precision::DAY (in older versions)
将 DateInterval
对象(如 CarbonInterval
)转换为存在的 Spatie 精度掩码,如果不存在,则抛出 RuntimeException
。
convertDateIntervalToPrecision
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
将 Spatie 精度掩码转换为 CarbonInterval
。