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
-
string
PERIOD_DAYS ¶'D'
-
string
PERIOD_HOURS ¶'H'
-
string
PERIOD_MINUTES ¶'M'
-
string
PERIOD_MONTHS ¶'M'
-
string
PERIOD_PREFIX ¶'P'
-
string
PERIOD_SECONDS ¶'S'
-
string
PERIOD_TIME_PREFIX ¶'T'
-
string
PERIOD_YEARS ¶'Y'
-
int
PHP_DAYS_FALSE ¶-99999
Before PHP 5.4.20/5.5.4 instead of
false
days will be set to -99999 when the interval instance was created by DateTime:diff().
Property Summary
-
$daysExcludeWeeks public @property-read
int
alias of dayzExcludeWeeks
-
$dayz public @property
int
Total days of the current interval (weeks * 7 + days).
-
$dayzExcludeWeeks public @property-read
int
Total days remaining in the final week of the current instance (days % 7).
-
$hours public @property
int
Total hours of the current interval.
-
$minutes public @property
int
Total minutes of the current interval.
-
$months public @property
int
Total months of the current interval.
-
$seconds public @property
int
Total seconds of the current interval.
-
$weeks public @property
int
Total weeks of the current interval calculated from the days.
-
$years public @property
int
Total 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
int
Throws
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
void
Throws
InvalidArgumentException
add() ¶ public
add(DateInterval $interval): static
Add the passed interval to the current instance
Parameters
-
DateInterval
$interval The interval to add.
Returns
static
create() ¶ 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
static
day() ¶ public @method
day(): ChronosInterval
day($days = 1) Alias for days.
Returns
ChronosInterval
days() ¶ public @method
days(): ChronosInterval
days($days = 1) Set the days portion of the current interval.
Returns
ChronosInterval
dayz() ¶ public @method
dayz(): ChronosInterval
dayz($days = 1) Alias for days.
Returns
ChronosInterval
hour() ¶ public @method
hour(): ChronosInterval
hour($hours = 1) Alias for hours.
Returns
ChronosInterval
hours() ¶ public @method
hours(): ChronosInterval
hours($hours = 1) Set the hours portion of the current interval.
Returns
ChronosInterval
instance() ¶ 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
static
Throws
InvalidArgumentException
minute() ¶ public @method
minute(): ChronosInterval
minute($minutes = 1) Alias for minutes.
Returns
ChronosInterval
minutes() ¶ public @method
minutes(): ChronosInterval
minutes($minutes = 1) Set the minutes portion of the current interval.
Returns
ChronosInterval
month() ¶ public @method
month(): ChronosInterval
month($months = 1) Alias for months.
Returns
ChronosInterval
months() ¶ public @method
months(): ChronosInterval
months($months = 1) Set the months portion of the current interval.
Returns
ChronosInterval
second() ¶ public @method
second(): ChronosInterval
second($seconds = 1) Alias for seconds.
Returns
ChronosInterval
seconds() ¶ public @method
seconds(): ChronosInterval
seconds($seconds = 1) Set the seconds portion of the current interval.
Returns
ChronosInterval
wasCreatedFromDiff() ¶ 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
bool
week() ¶ public @method
week(): ChronosInterval
week($weeks = 1) Alias for weeks.
Returns
ChronosInterval
weeks() ¶ public @method
weeks(): ChronosInterval
weeks($weeks = 1) Set the weeks portion of the current interval. Will overwrite dayz value.
Returns
ChronosInterval
weeksAndDays() ¶ 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
static
year() ¶ public @method
year(): ChronosInterval
year($years = 1) Alias for years.
Returns
ChronosInterval
years() ¶ public @method
years(): ChronosInterval
years($years = 1) Set the years portion of the current interval.
Returns
ChronosInterval
Property Detail
$dayzExcludeWeeks ¶ public @property-read
Total days remaining in the final week of the current instance (days % 7).
Type
int