ChronosInterval
extends DateInterval
in package
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.
Table of Contents
Constants
- PERIOD_DAYS = 'D'
- PERIOD_HOURS = 'H'
- PERIOD_MINUTES = 'M'
- PERIOD_MONTHS = 'M'
- PERIOD_PREFIX = 'P'
- PERIOD_SECONDS = 'S'
- PERIOD_TIME_PREFIX = 'T'
- PERIOD_YEARS = 'Y'
Properties
- $daysExcludeWeeks : int
- $dayz : int
- $dayzExcludeWeeks : int
- $hours : int
- $microseconds : int
- $minutes : int
- $months : int
- $seconds : int
- $weeks : int
- $years : int
Methods
- __call() : $this
- Allow fluent calls on the setters... ChronosInterval::years(3)->months(5)->day().
- __callStatic() : static
- Provide static helpers to create instances. Allows:
- __construct() : mixed
- Create a new ChronosInterval instance.
- __get() : int
- Get a part of the ChronosInterval object
- __set() : void
- Set a part of the ChronosInterval object
- __toString() : string
- Returns the ISO 8601 interval string.
- add() : $this
- Add the passed interval to the current instance
- create() : static
- Create a new ChronosInterval instance from specific values.
- day() : ChronosInterval
- days() : ChronosInterval
- dayz() : ChronosInterval
- hour() : ChronosInterval
- hours() : ChronosInterval
- instance() : 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.
- microsecond() : ChronosInterval
- microseconds() : ChronosInterval
- minute() : ChronosInterval
- minutes() : ChronosInterval
- month() : ChronosInterval
- months() : ChronosInterval
- second() : ChronosInterval
- seconds() : ChronosInterval
- week() : ChronosInterval
- weeks() : ChronosInterval
- weeksAndDays() : $this
- Allow setting of weeks and days to be cumulative.
- year() : ChronosInterval
- years() : ChronosInterval
- wasCreatedFromDiff() : bool
- Determine if the interval was created via DateTime:diff() or not.
Constants
PERIOD_DAYS
public
string
PERIOD_DAYS
= 'D'
PERIOD_HOURS
public
string
PERIOD_HOURS
= 'H'
PERIOD_MINUTES
public
string
PERIOD_MINUTES
= 'M'
PERIOD_MONTHS
public
string
PERIOD_MONTHS
= 'M'
PERIOD_PREFIX
public
string
PERIOD_PREFIX
= 'P'
PERIOD_SECONDS
public
string
PERIOD_SECONDS
= 'S'
PERIOD_TIME_PREFIX
public
string
PERIOD_TIME_PREFIX
= 'T'
PERIOD_YEARS
public
string
PERIOD_YEARS
= 'Y'
Properties
$daysExcludeWeeks read-only
public
int
$daysExcludeWeeks
alias of dayzExcludeWeeks
$dayz
public
int
$dayz
Total days of the current interval (weeks * 7 + days).
$dayzExcludeWeeks read-only
public
int
$dayzExcludeWeeks
Total days remaining in the final week of the current instance (days % 7).
$hours
public
int
$hours
Total hours of the current interval.
$microseconds
public
int
$microseconds
Total microseconds of the current interval.
$minutes
public
int
$minutes
Total minutes of the current interval.
$months
public
int
$months
Total months of the current interval.
$seconds
public
int
$seconds
Total seconds of the current interval.
$weeks
public
int
$weeks
Total weeks of the current interval calculated from the days.
$years
public
int
$years
Total years of the current interval.
Methods
__call()
Allow fluent calls on the setters... ChronosInterval::years(3)->months(5)->day().
public
__call(string $name, array<string|int, mixed> $args) : $this
Note: This is done using the magic method to allow static and instance methods to have the same names.
Parameters
- $name : string
-
The property name to augment. Accepts plural forms in addition to singular ones.
- $args : array<string|int, mixed>
-
The value to set.
Return values
$this__callStatic()
Provide static helpers to create instances. Allows:
public
static __callStatic(string $name, array<string|int, mixed> $args) : static
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
- $name : string
-
The property to configure. Accepts singular and plural forms.
- $args : array<string|int, mixed>
-
Contains the value to use.
Return values
static__construct()
Create a new ChronosInterval instance.
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 ][, int|null $microseconds = null ]) : mixed
Parameters
- $years : int|null = 1
-
The year to use.
- $months : int|null = null
-
The month to use.
- $weeks : int|null = null
-
The week to use.
- $days : int|null = null
-
The day to use.
- $hours : int|null = null
-
The hours to use.
- $minutes : int|null = null
-
The minutes to use.
- $seconds : int|null = null
-
The seconds to use.
- $microseconds : int|null = null
-
The microseconds to use.
__get()
Get a part of the ChronosInterval object
public
__get(string $name) : int
Parameters
- $name : string
-
The property to read.
Tags
Return values
int__set()
Set a part of the ChronosInterval object
public
__set(string $name, int $val) : void
Parameters
- $name : string
-
The property to augment.
- $val : int
-
The value to change.
Tags
__toString()
Returns the ISO 8601 interval string.
public
__toString() : string
Return values
string —Interval as string representation
add()
Add the passed interval to the current instance
public
add(DateInterval $interval) : $this
Parameters
- $interval : DateInterval
-
The interval to add.
Return values
$thiscreate()
Create a new ChronosInterval instance from specific values.
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 ][, int|null $microseconds = null ]) : static
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
- $years : int|null = 1
-
The year to use.
- $months : int|null = null
-
The month to use.
- $weeks : int|null = null
-
The week to use.
- $days : int|null = null
-
The day to use.
- $hours : int|null = null
-
The hours to use.
- $minutes : int|null = null
-
The minutes to use.
- $seconds : int|null = null
-
The seconds to use.
- $microseconds : int|null = null
-
The microseconds to use.
Return values
staticday()
public
static day([mixed $days = 1 ]) : ChronosInterval
Alias for days
Parameters
- $days : mixed = 1
Return values
ChronosIntervaldays()
public
static days([mixed $days = 1 ]) : ChronosInterval
Create instance specifying a number of days.
Parameters
- $days : mixed = 1
Return values
ChronosIntervaldayz()
public
static dayz([mixed $days = 1 ]) : ChronosInterval
Alias for days
Parameters
- $days : mixed = 1
Return values
ChronosIntervalhour()
public
static hour([mixed $hours = 1 ]) : ChronosInterval
Alias for hours
Parameters
- $hours : mixed = 1
Return values
ChronosIntervalhours()
public
static hours([mixed $hours = 1 ]) : ChronosInterval
Create instance specifying a number of hours.
Parameters
- $hours : mixed = 1
Return values
ChronosIntervalinstance()
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.
public
static instance(DateInterval $di) : static
Parameters
- $di : DateInterval
-
The DateInterval instance to copy.
Tags
Return values
staticmicrosecond()
public
static microsecond([mixed $microseconds = 1 ]) : ChronosInterval
Alias for microseconds
Parameters
- $microseconds : mixed = 1
Return values
ChronosIntervalmicroseconds()
public
static microseconds([mixed $microseconds = 1 ]) : ChronosInterval
Create instance specifying a number of microseconds.
Parameters
- $microseconds : mixed = 1
Return values
ChronosIntervalminute()
public
static minute([mixed $minutes = 1 ]) : ChronosInterval
Alias for minutes
Parameters
- $minutes : mixed = 1
Return values
ChronosIntervalminutes()
public
static minutes([mixed $minutes = 1 ]) : ChronosInterval
Create instance specifying a number of minutes.
Parameters
- $minutes : mixed = 1
Return values
ChronosIntervalmonth()
public
static month([mixed $months = 1 ]) : ChronosInterval
Alias for months
Parameters
- $months : mixed = 1
Return values
ChronosIntervalmonths()
public
static months([mixed $months = 1 ]) : ChronosInterval
Create instance specifying a number of months.
Parameters
- $months : mixed = 1
Return values
ChronosIntervalsecond()
public
static second([mixed $seconds = 1 ]) : ChronosInterval
Alias for seconds
Parameters
- $seconds : mixed = 1
Return values
ChronosIntervalseconds()
public
static seconds([mixed $seconds = 1 ]) : ChronosInterval
Create instance specifying a number of seconds.
Parameters
- $seconds : mixed = 1
Return values
ChronosIntervalweek()
public
static week([mixed $weeks = 1 ]) : ChronosInterval
Alias for weeks
Parameters
- $weeks : mixed = 1
Return values
ChronosIntervalweeks()
public
static weeks([mixed $weeks = 1 ]) : ChronosInterval
Create instance specifying a number of weeks.
Parameters
- $weeks : mixed = 1
Return values
ChronosIntervalweeksAndDays()
Allow setting of weeks and days to be cumulative.
public
weeksAndDays(int $weeks, int $days) : $this
Parameters
- $weeks : int
-
Number of weeks to set
- $days : int
-
Number of days to set
Return values
$thisyear()
public
static year([mixed $years = 1 ]) : ChronosInterval
Alias for years
Parameters
- $years : mixed = 1
Return values
ChronosIntervalyears()
public
static years([mixed $years = 1 ]) : ChronosInterval
Create instance specifying a number of years.
Parameters
- $years : mixed = 1
Return values
ChronosIntervalwasCreatedFromDiff()
Determine if the interval was created via DateTime:diff() or not.
protected
static wasCreatedFromDiff(DateInterval $interval) : bool
Parameters
- $interval : DateInterval
-
The interval to check.