Class ChronosInterval
A wrapper around DateInterval that provides additional convenience methods.
This class uses the decorator pattern to wrap a native DateInterval, providing ISO 8601 duration string formatting and factory methods.
Property Summary
-
$d public @property-read
intDays
-
$days public @property-read
int|falseTotal days if created from diff(), false otherwise
-
$f public @property-read
floatMicroseconds as a fraction of a second
-
$h public @property-read
intHours
-
$i public @property-read
intMinutes
-
$interval protected
DateIntervalThe wrapped DateInterval instance.
-
$invert public @property-read
int1 if the interval is negative
-
$m public @property-read
intMonths
-
$s public @property-read
intSeconds
-
$y public @property-read
intYears
Method Summary
-
__construct() public
Create a new ChronosInterval instance.
-
__debugInfo() public
Debug info.
-
__get() public
Allow read access to DateInterval properties.
-
__isset() public
Check if a DateInterval property exists.
-
__toString() public
Return the interval as an ISO 8601 duration string.
-
add() public
Add another interval to this one.
-
create() public static
Create an interval from a specification string.
-
createFromDateString() public static
Create an interval from a relative date string.
-
createFromValues() public static
Create an interval from individual components.
-
format() public
Format the interval using DateInterval::format().
-
instance() public static
Create an interval from a DateInterval instance.
-
isNegative() public
Check if this interval is negative.
-
isZero() public
Check if this interval is zero (no duration).
-
sub() public
Subtract another interval from this one.
-
toDateString() public
Format the interval as a strtotime()-compatible string.
-
toIso8601String() public
Format the interval as an ISO 8601 duration string.
-
toNative() public
Get the underlying DateInterval instance.
-
totalDays() public
Get the total number of days in the interval.
-
totalSeconds() public
Get the total number of seconds in the interval.
Method Detail
__construct() ¶ public
__construct(DateInterval $interval)
Create a new ChronosInterval instance.
Parameters
-
DateInterval$interval The interval to wrap.
__get() ¶ public
__get(string $name): mixed
Allow read access to DateInterval properties.
Parameters
-
string$name Property name.
Returns
mixed__isset() ¶ public
__isset(string $name): bool
Check if a DateInterval property exists.
Parameters
-
string$name Property name.
Returns
bool__toString() ¶ public
__toString(): string
Return the interval as an ISO 8601 duration string.
Returns
stringadd() ¶ public
add(DateInterval|Cake\Chronos\ChronosInterval $interval): static
Add another interval to this one.
Returns a new ChronosInterval with the combined values. Note: This performs simple addition of each component and does not normalize overflow (e.g., 70 minutes stays as 70 minutes).
Parameters
-
DateInterval|Cake\Chronos\ChronosInterval$interval The interval to add.
Returns
staticcreate() ¶ public static
create(string $spec): static
Create an interval from a specification string.
Parameters
-
string$spec An interval specification (e.g., 'P1Y2M3D').
Returns
staticcreateFromDateString() ¶ public static
createFromDateString(string $datetime): static
Create an interval from a relative date string.
This wraps DateInterval::createFromDateString() which accepts relative date/time formats like "1 year + 2 days" or "3 months".
Parameters
-
string$datetime A relative date/time string.
Returns
staticThrows
InvalidArgumentExceptionIf the string cannot be parsed.
See Also
createFromValues() ¶ public static
createFromValues(int|null $years = null, 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
Create an interval from individual components.
Parameters
-
int|null$years optional Years
-
int|null$months optional Months
-
int|null$weeks optional Weeks (converted to days)
-
int|null$days optional Days
-
int|null$hours optional Hours
-
int|null$minutes optional Minutes
-
int|null$seconds optional Seconds
-
int|null$microseconds optional Microseconds
Returns
staticformat() ¶ public
format(string $format): string
Format the interval using DateInterval::format().
Parameters
-
string$format The format string.
Returns
stringSee Also
instance() ¶ public static
instance(DateInterval $interval): static
Create an interval from a DateInterval instance.
Parameters
-
DateInterval$interval The interval to wrap.
Returns
staticsub() ¶ public
sub(DateInterval|Cake\Chronos\ChronosInterval $interval): static
Subtract another interval from this one.
Returns a new ChronosInterval with the subtracted values. Note: This performs simple subtraction of each component. If any component becomes negative, the result may be unexpected.
Parameters
-
DateInterval|Cake\Chronos\ChronosInterval$interval The interval to subtract.
Returns
statictoDateString() ¶ public
toDateString(): string
Format the interval as a strtotime()-compatible string.
Returns a relative date/time string that can be used with strtotime() or DateInterval::createFromDateString().
Returns
stringtoIso8601String() ¶ public
toIso8601String(): string
Format the interval as an ISO 8601 duration string.
Returns
stringtoNative() ¶ public
toNative(): DateInterval
Get the underlying DateInterval instance.
Use this when you need to pass the interval to code that expects a native DateInterval.
Returns
DateIntervaltotalDays() ¶ public
totalDays(): int
Get the total number of days in the interval.
If the interval was created from a diff(), this returns the exact total days. Otherwise, it approximates using 30 days per month and 365 days per year.
Returns
inttotalSeconds() ¶ public
totalSeconds(): int
Get the total number of seconds in the interval.
Note: This calculation assumes 30 days per month and 365 days per year, which is an approximation. For precise calculations, use diff() between specific dates.
Returns
int