Fr3nch13/CakePHP Utilities

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

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).

$microseconds

public int $microseconds

Total microseconds of the current interval.

$weeks

public int $weeks

Total weeks of the current interval calculated from the days.

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
throws
InvalidArgumentException
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
throws
InvalidArgumentException

__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
$this

create()

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
static

instance()

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
throws
InvalidArgumentException
Return values
static

weeksAndDays()

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
$this

wasCreatedFromDiff()

Determine if the interval was created via DateTime:diff() or not.

protected static wasCreatedFromDiff(DateInterval $interval) : bool
Parameters
$interval : DateInterval

The interval to check.

Return values
bool

        
On this page

Search results