Class ChronosInterval
A simple API extension for DateInterval. The implementation provides helpers to handle weeks but only days are saved. Weeks are calculated based on the total days of the current instance.
Constants
-
stringPERIOD_DAYS ¶'D' -
stringPERIOD_HOURS ¶'H' -
stringPERIOD_MINUTES ¶'M' -
stringPERIOD_MONTHS ¶'M' -
stringPERIOD_PREFIX ¶'P' -
stringPERIOD_SECONDS ¶'S' -
stringPERIOD_TIME_PREFIX ¶'T' -
stringPERIOD_YEARS ¶'Y' -
intPHP_DAYS_FALSE ¶-99999Before PHP 5.4.20/5.5.4 instead of
falsedays will be set to -99999 when the interval instance was created by DateTime:diff().
Property Summary
-
$daysExcludeWeeks public @property-read
intalias of dayzExcludeWeeks
-
$dayz public @property
intTotal days of the current interval (weeks * 7 + days).
-
$dayzExcludeWeeks public @property-read
intTotal days remaining in the final week of the current instance (days % 7).
-
$hours public @property
intTotal hours of the current interval.
-
$minutes public @property
intTotal minutes of the current interval.
-
$months public @property
intTotal months of the current interval.
-
$seconds public @property
intTotal seconds of the current interval.
-
$weeks public @property
intTotal weeks of the current interval calculated from the days.
-
$years public @property
intTotal years of the current interval.
Method Summary
-
__call() public
Allow fluent calls on the setters... ChronosInterval::years(3)->months(5)->day().
-
__callStatic() public static
Provide static helpers to create instances. Allows:
-
__construct() public
Create a new ChronosInterval instance.
-
__get() public
Get a part of the ChronosInterval object
-
__set() public
Set a part of the ChronosInterval object
-
__toString() public
Returns the ISO 8601 interval string.
-
add() public
Add the passed interval to the current instance
-
create() public static
Create a new ChronosInterval instance from specific values. This is an alias for the constructor that allows better fluent syntax as it allows you to do ChronosInterval::create(1)->fn() rather than (new ChronosInterval(1))->fn().
-
day() public @method
day($days = 1) Alias for days.
-
days() public @method
days($days = 1) Set the days portion of the current interval.
-
dayz() public @method
dayz($days = 1) Alias for days.
-
hour() public @method
hour($hours = 1) Alias for hours.
-
hours() public @method
hours($hours = 1) Set the hours portion of the current interval.
-
instance() public static
Create a ChronosInterval instance from a DateInterval one. Can not instance DateInterval objects created from DateTime::diff() as you can't externally set the $days field.
-
minute() public @method
minute($minutes = 1) Alias for minutes.
-
minutes() public @method
minutes($minutes = 1) Set the minutes portion of the current interval.
-
month() public @method
month($months = 1) Alias for months.
-
months() public @method
months($months = 1) Set the months portion of the current interval.
-
second() public @method
second($seconds = 1) Alias for seconds.
-
seconds() public @method
seconds($seconds = 1) Set the seconds portion of the current interval.
-
wasCreatedFromDiff() protected static
Determine if the interval was created via DateTime:diff() or not.
-
week() public @method
week($weeks = 1) Alias for weeks.
-
weeks() public @method
weeks($weeks = 1) Set the weeks portion of the current interval. Will overwrite dayz value.
-
weeksAndDays() public
Allow setting of weeks and days to be cumulative.
-
year() public @method
year($years = 1) Alias for years.
-
years() public @method
years($years = 1) Set the years portion of the current interval.
Method Detail
__call() ¶ public
__call(string $name, array $args): static
Allow fluent calls on the setters... ChronosInterval::years(3)->months(5)->day().
Note: This is done using the magic method to allow static and instance methods to have the same names.
Parameters
-
string$name The property name to augment. Accepts plural forms in addition to singular ones.
-
array$args The value to set.
Returns
static__callStatic() ¶ public static
__callStatic(string $name, array $args): static
Provide static helpers to create instances. Allows:
ChronosInterval::years(3)
// or
ChronosInterval::month(1);
Note: This is done using the magic method to allow static and instance methods to have the same names.
Parameters
-
string$name The property to configure. Accepts singular and plural forms.
-
array$args Contains the value to use.
Returns
static__construct() ¶ public
__construct(int|null $years = 1, int|null $months = null, int|null $weeks = null, int|null $days = null, int|null $hours = null, int|null $minutes = null, int|null $seconds = null)
Create a new ChronosInterval instance.
Parameters
-
int|null$years optional The year to use.
-
int|null$months optional The month to use.
-
int|null$weeks optional The week to use.
-
int|null$days optional The day to use.
-
int|null$hours optional The hours to use.
-
int|null$minutes optional The minutes to use.
-
int|null$seconds optional The seconds to use.
__get() ¶ public
__get(string $name): int
Get a part of the ChronosInterval object
Parameters
-
string$name The property to read.
Returns
intThrows
InvalidArgumentException__set() ¶ public
__set(string $name, int $val): void
Set a part of the ChronosInterval object
Parameters
-
string$name The property to augment.
-
int$val The value to change.
Returns
voidThrows
InvalidArgumentException__toString() ¶ public
__toString(): string
Returns the ISO 8601 interval string.
Returns
stringInterval as string representation
add() ¶ public
add(DateInterval $interval): static
Add the passed interval to the current instance
Parameters
-
DateInterval$interval The interval to add.
Returns
staticcreate() ¶ public static
create(int|null $years = 1, int|null $months = null, int|null $weeks = null, int|null $days = null, int|null $hours = null, int|null $minutes = null, int|null $seconds = null): static
Create a new ChronosInterval instance from specific values. This is an alias for the constructor that allows better fluent syntax as it allows you to do ChronosInterval::create(1)->fn() rather than (new ChronosInterval(1))->fn().
Parameters
-
int|null$years optional The year to use.
-
int|null$months optional The month to use.
-
int|null$weeks optional The week to use.
-
int|null$days optional The day to use.
-
int|null$hours optional The hours to use.
-
int|null$minutes optional The minutes to use.
-
int|null$seconds optional The seconds to use.
Returns
staticday() ¶ public @method
day(): ChronosInterval
day($days = 1) Alias for days.
Returns
ChronosIntervaldays() ¶ public @method
days(): ChronosInterval
days($days = 1) Set the days portion of the current interval.
Returns
ChronosIntervaldayz() ¶ public @method
dayz(): ChronosInterval
dayz($days = 1) Alias for days.
Returns
ChronosIntervalhour() ¶ public @method
hour(): ChronosInterval
hour($hours = 1) Alias for hours.
Returns
ChronosIntervalhours() ¶ public @method
hours(): ChronosInterval
hours($hours = 1) Set the hours portion of the current interval.
Returns
ChronosIntervalinstance() ¶ public static
instance(DateInterval $di): static
Create a ChronosInterval instance from a DateInterval one. Can not instance DateInterval objects created from DateTime::diff() as you can't externally set the $days field.
Parameters
-
DateInterval$di The DateInterval instance to copy.
Returns
staticThrows
InvalidArgumentExceptionminute() ¶ public @method
minute(): ChronosInterval
minute($minutes = 1) Alias for minutes.
Returns
ChronosIntervalminutes() ¶ public @method
minutes(): ChronosInterval
minutes($minutes = 1) Set the minutes portion of the current interval.
Returns
ChronosIntervalmonth() ¶ public @method
month(): ChronosInterval
month($months = 1) Alias for months.
Returns
ChronosIntervalmonths() ¶ public @method
months(): ChronosInterval
months($months = 1) Set the months portion of the current interval.
Returns
ChronosIntervalsecond() ¶ public @method
second(): ChronosInterval
second($seconds = 1) Alias for seconds.
Returns
ChronosIntervalseconds() ¶ public @method
seconds(): ChronosInterval
seconds($seconds = 1) Set the seconds portion of the current interval.
Returns
ChronosIntervalwasCreatedFromDiff() ¶ protected static
wasCreatedFromDiff(DateInterval $interval): bool
Determine if the interval was created via DateTime:diff() or not.
Parameters
-
DateInterval$interval The interval to check.
Returns
boolweek() ¶ public @method
week(): ChronosInterval
week($weeks = 1) Alias for weeks.
Returns
ChronosIntervalweeks() ¶ public @method
weeks(): ChronosInterval
weeks($weeks = 1) Set the weeks portion of the current interval. Will overwrite dayz value.
Returns
ChronosIntervalweeksAndDays() ¶ public
weeksAndDays(int $weeks, int $days): static
Allow setting of weeks and days to be cumulative.
Parameters
-
int$weeks Number of weeks to set
-
int$days Number of days to set
Returns
staticyear() ¶ public @method
year(): ChronosInterval
year($years = 1) Alias for years.
Returns
ChronosIntervalyears() ¶ public @method
years(): ChronosInterval
years($years = 1) Set the years portion of the current interval.
Returns
ChronosIntervalProperty Detail
$dayzExcludeWeeks ¶ public @property-read
Total days remaining in the final week of the current instance (days % 7).
Type
int