CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C Chronos 3.x API

  • Project:
    • Chronos
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 3.x
      • 3.x
      • 2.x
      • 1.x

Namespaces

  • Cake\Chronos

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.

Namespace: Cake\Chronos

Property Summary

  • $d public @property-read
    int

    Days

  • $days public @property-read
    int|false

    Total days if created from diff(), false otherwise

  • $f public @property-read
    float

    Microseconds as a fraction of a second

  • $h public @property-read
    int

    Hours

  • $i public @property-read
    int

    Minutes

  • $interval protected
    DateInterval

    The wrapped DateInterval instance.

  • $invert public @property-read
    int

    1 if the interval is negative

  • $m public @property-read
    int

    Months

  • $s public @property-read
    int

    Seconds

  • $y public @property-read
    int

    Years

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.

__debugInfo() ¶ public

__debugInfo(): array<string, mixed>

Debug info.

Returns
array<string, mixed>

__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
string

add() ¶ 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
static

create() ¶ public static

create(string $spec): static

Create an interval from a specification string.

Parameters
string $spec

An interval specification (e.g., 'P1Y2M3D').

Returns
static

createFromDateString() ¶ 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
static
Throws
InvalidArgumentException
If the string cannot be parsed.
See Also
https://www.php.net/manual/en/dateinterval.createfromdatestring.php

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
static

format() ¶ public

format(string $format): string

Format the interval using DateInterval::format().

Parameters
string $format

The format string.

Returns
string
See Also
https://www.php.net/manual/en/dateinterval.format.php

instance() ¶ public static

instance(DateInterval $interval): static

Create an interval from a DateInterval instance.

Parameters
DateInterval $interval

The interval to wrap.

Returns
static

isNegative() ¶ public

isNegative(): bool

Check if this interval is negative.

Returns
bool

isZero() ¶ public

isZero(): bool

Check if this interval is zero (no duration).

Returns
bool

sub() ¶ 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
static

toDateString() ¶ 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
string

toIso8601String() ¶ public

toIso8601String(): string

Format the interval as an ISO 8601 duration string.

Returns
string

toNative() ¶ 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
DateInterval

totalDays() ¶ 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
int

totalSeconds() ¶ 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

Property Detail

$d ¶ public @property-read

Days

Type
int

$days ¶ public @property-read

Total days if created from diff(), false otherwise

Type
int|false

$f ¶ public @property-read

Microseconds as a fraction of a second

Type
float

$h ¶ public @property-read

Hours

Type
int

$i ¶ public @property-read

Minutes

Type
int

$interval ¶ protected

The wrapped DateInterval instance.

Type
DateInterval

$invert ¶ public @property-read

1 if the interval is negative

Type
int

$m ¶ public @property-read

Months

Type
int

$s ¶ public @property-read

Seconds

Type
int

$y ¶ public @property-read

Years

Type
int
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs