\Cake\I18nTime

Extends the built-in DateTime class to provide handy methods and locale-aware formatting helpers

This object can be mutated in place using any setter method, or __set().

Summary

Methods
Properties
Constants
getDefaultLocale()
setDefaultLocale()
nice()
i18nFormat()
__toString()
resetToStringFormat()
setToStringFormat()
setJsonEncodeFormat()
parseDateTime()
parseDate()
parseTime()
jsonSerialize()
diffFormatter()
__debugInfo()
__construct()
toImmutable()
__set()
getWeekendDays()
setWeekendDays()
eq()
ne()
gt()
gte()
lt()
lte()
between()
closest()
farthest()
min()
max()
isWeekday()
isWeekend()
isYesterday()
isToday()
isTomorrow()
isNextWeek()
isLastWeek()
isNextMonth()
isLastMonth()
isNextYear()
isLastYear()
isFuture()
isPast()
isLeapYear()
isSameDay()
isSunday()
isMonday()
isTuesday()
isWednesday()
isThursday()
isFriday()
isSaturday()
isThisWeek()
isThisMonth()
isThisYear()
isBirthday()
wasWithinLast()
isWithinNext()
isMutable()
diffInYears()
diffInMonths()
diffInWeeks()
diffInDays()
diffInDaysFiltered()
diffInHoursFiltered()
diffFiltered()
diffInWeekdays()
diffInWeekendDays()
diffInHours()
diffInMinutes()
diffInSeconds()
secondsSinceMidnight()
secondsUntilEndOfDay()
fromNow()
diffForHumans()
instance()
parse()
now()
today()
tomorrow()
yesterday()
maxValue()
minValue()
create()
createFromDate()
createFromTime()
createFromFormat()
createFromTimestamp()
createFromTimestampUTC()
getLastErrors()
toDateString()
toFormattedDateString()
toTimeString()
toDateTimeString()
toDayDateTimeString()
toAtomString()
toCookieString()
toIso8601String()
toRfc822String()
toRfc850String()
toRfc1036String()
toRfc1123String()
toRfc2822String()
toRfc3339String()
toRssString()
toW3cString()
toUnixString()
toQuarter()
toWeek()
__get()
__isset()
getWeekStartsAt()
setWeekStartsAt()
getWeekEndsAt()
setWeekEndsAt()
setDate()
setDateTime()
setTimeFromTimeString()
timestamp()
year()
month()
day()
hour()
minute()
second()
addYears()
addYear()
subYear()
subYears()
addMonths()
addMonth()
subMonth()
subMonths()
addMonthsWithOverflow()
addMonthWithOverflow()
subMonthWithOverflow()
subMonthsWithOverflow()
addDays()
addDay()
subDay()
subDays()
addWeekdays()
addWeekday()
subWeekdays()
subWeekday()
addWeeks()
addWeek()
subWeek()
subWeeks()
addHours()
addHour()
subHour()
subHours()
addMinutes()
addMinute()
subMinute()
subMinutes()
addSeconds()
addSecond()
subSecond()
subSeconds()
startOfDay()
endOfDay()
startOfMonth()
endOfMonth()
startOfYear()
endOfYear()
startOfDecade()
endOfDecade()
startOfCentury()
endOfCentury()
startOfWeek()
endOfWeek()
next()
previous()
firstOfMonth()
lastOfMonth()
nthOfMonth()
firstOfQuarter()
lastOfQuarter()
nthOfQuarter()
firstOfYear()
lastOfYear()
nthOfYear()
average()
hasRelativeKeywords()
setTestNow()
getTestNow()
hasTestNow()
timezone()
tz()
setTimezone()
timeAgoInWords()
listTimezones()
$defaultLocale
$niceFormat
$wordFormat
$wordAccuracy
$wordEnd
$year
$yearIso
$month
$day
$hour
$minute
$second
$timestamp
$timezone
$tz
$micro
$dayOfWeek
$dayOfYear
$weekOfMonth
$weekOfYear
$daysInMonth
$age
$quarter
$offset
$offsetHours
$dst
$local
$utc
$timezoneName
$tzName
UNIX_TIMESTAMP_FORMAT
_formatObject()
safeCreateDateTimeZone()
$_formatters
$_jsonEncodeFormat
$_isDateInstance
$toStringFormat
$weekendDays
$diffFormatter
$_lastErrors
$days
$weekStartsAt
$weekEndsAt
$relativePattern
$_toStringFormat
N/A
setDateParent()
No private properties found
N/A

Constants

UNIX_TIMESTAMP_FORMAT

UNIX_TIMESTAMP_FORMAT = 'unixTimestampFormat' : string

serialise the value as a Unix Timestamp

Properties

$defaultLocale

$defaultLocale : string

The default locale to be used for displaying formatted date strings.

Type

string

$niceFormat

$niceFormat : string|array|integer

The format to use when formatting a time using `Cake\I18n\Time::nice()`

The format should be either the formatting constants from IntlDateFormatter as described in (https://secure.php.net/manual/en/class.intldateformatter.php) or a pattern as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)

It is possible to provide an array of 2 constants. In this case, the first position will be used for formatting the date part of the object and the second position will be used to format the time part.

Type

string|array|integer

$wordFormat

$wordFormat : string|array|integer

The format to use when formatting a time using `Cake\I18n\Time::timeAgoInWords()` and the difference is more than `Cake\I18n\Time::$wordEnd`

Type

string|array|integer

$wordAccuracy

$wordAccuracy : array

The format to use when formatting a time using `Time::timeAgoInWords()` and the difference is less than `Time::$wordEnd`

Type

array

$wordEnd

$wordEnd : string

The end of relative time telling

Type

string

$year

$year : integer

Type

integer

$yearIso

$yearIso : integer

Type

integer

$month

$month : integer

Type

integer

$day

$day : integer

Type

integer

$hour

$hour : integer

Type

integer

$minute

$minute : integer

Type

integer

$second

$second : integer

Type

integer

$timestamp

$timestamp : integer

seconds since the Unix Epoch

Type

integer

$timezone

$timezone : \DateTimeZone

the current timezone

Type

\DateTimeZone

$tz

$tz : \DateTimeZone

alias of timezone

Type

\DateTimeZone

$micro

$micro : integer

Type

integer

$dayOfWeek

$dayOfWeek : integer

1 (for Monday) through 7 (for Sunday)

Type

integer

$dayOfYear

$dayOfYear : integer

0 through 365

Type

integer

$weekOfMonth

$weekOfMonth : integer

1 through 5

Type

integer

$weekOfYear

$weekOfYear : integer

ISO-8601 week number of year, weeks starting on Monday

Type

integer

$daysInMonth

$daysInMonth : integer

number of days in the given month

Type

integer

$age

$age : integer

does a diffInYears() with default parameters

Type

integer

$quarter

$quarter : integer

the quarter of this instance, 1 - 4

Type

integer

$offset

$offset : integer

the timezone offset in seconds from UTC

Type

integer

$offsetHours

$offsetHours : integer

the timezone offset in hours from UTC

Type

integer

$dst

$dst : boolean

daylight savings time indicator, true if DST, false otherwise

Type

boolean

$local

$local : boolean

checks if the timezone is local, true if local, false otherwise

Type

boolean

$utc

$utc : boolean

checks if the timezone is UTC, true if UTC, false otherwise

Type

boolean

$timezoneName

$timezoneName : string

Type

string

$tzName

$tzName : string

Type

string

$_formatters

$_formatters : array<mixed,\IntlDateFormatter>

In-memory cache of date formatters

Type

array<mixed,\IntlDateFormatter>

$_jsonEncodeFormat

$_jsonEncodeFormat : string|array|integer

The format to use when when converting this object to json

The format should be either the formatting constants from IntlDateFormatter as described in (https://secure.php.net/manual/en/class.intldateformatter.php) or a pattern as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)

It is possible to provide an array of 2 constants. In this case, the first position will be used for formatting the date part of the object and the second position will be used to format the time part.

Type

string|array|integer

$_isDateInstance

$_isDateInstance : boolean

Caches whether or not this class is a subclass of a Date or MutableDate

Type

boolean

$toStringFormat

$toStringFormat : string

Format to use for __toString method when type juggling occurs.

Type

string

$weekendDays

$weekendDays : array

Days of weekend

Type

array

$_lastErrors

$_lastErrors : array

Holds the last error generated by createFromFormat

Type

array

$days

$days : array

Names of days of the week.

Type

array

$weekStartsAt

$weekStartsAt : integer

First day of week

Type

integer

$weekEndsAt

$weekEndsAt : integer

Last day of week

Type

integer

$relativePattern

$relativePattern : 

Type

$_toStringFormat

$_toStringFormat : string|array|integer

The format to use when formatting a time using `Cake\I18n\Time::i18nFormat()` and `__toString`

The format should be either the formatting constants from IntlDateFormatter as described in (https://secure.php.net/manual/en/class.intldateformatter.php) or a pattern as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)

It is possible to provide an array of 2 constants. In this case, the first position will be used for formatting the date part of the object and the second position will be used to format the time part.

Type

string|array|integer

Methods

getDefaultLocale()

getDefaultLocale() : string|null

Gets the default locale.

Returns

string|null —

The default locale string to be used or null.

setDefaultLocale()

setDefaultLocale(string|null  $locale = null) : void

Sets the default locale.

Parameters

string|null $locale

The default locale string to be used or null.

nice()

nice(string|\DateTimeZone|null  $timezone = null, string|null  $locale = null) : string

Returns a nicely formatted date string for this object.

The format to be used is stored in the static property Time::niceFormat.

Parameters

string|\DateTimeZone|null $timezone

Timezone string or DateTimeZone object in which the date will be displayed. The timezone stored for this object will not be changed.

string|null $locale

The locale name in which the date should be displayed (e.g. pt-BR)

Returns

string —

Formatted date string

i18nFormat()

i18nFormat(string|integer|null  $format = null, string|\DateTimeZone|null  $timezone = null, string|null  $locale = null) : string|integer

Returns a formatted string for this time object using the preferred format and language for the specified locale.

It is possible to specify the desired format for the string to be displayed. You can either pass IntlDateFormatter constants as the first argument of this function, or pass a full ICU date formatting string as specified in the following resource: http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details.

Additional to IntlDateFormatter constants and date formatting string you can use Time::UNIX_TIMESTAMP_FORMAT to get a unix timestamp

Examples

$time = new Time('2014-04-20 22:10');
$time->i18nFormat(); // outputs '4/20/14, 10:10 PM' for the en-US locale
$time->i18nFormat(\IntlDateFormatter::FULL); // Use the full date and time format
$time->i18nFormat([\IntlDateFormatter::FULL, \IntlDateFormatter::SHORT]); // Use full date but short time format
$time->i18nFormat('yyyy-MM-dd HH:mm:ss'); // outputs '2014-04-20 22:10'
$time->i18nFormat(Time::UNIX_TIMESTAMP_FORMAT); // outputs '1398031800'

If you wish to control the default format to be used for this method, you can alter the value of the static Time::$defaultLocale variable and set it to one of the possible formats accepted by this function.

You can read about the available IntlDateFormatter constants at https://secure.php.net/manual/en/class.intldateformatter.php

If you need to display the date in a different timezone than the one being used for this Time object without altering its internal state, you can pass a timezone string or object as the second parameter.

Finally, should you need to use a different locale for displaying this time object, pass a locale string as the third parameter to this function.

Examples

$time = new Time('2014-04-20 22:10');
$time->i18nFormat(null, null, 'de-DE');
$time->i18nFormat(\IntlDateFormatter::FULL, 'Europe/Berlin', 'de-DE');

You can control the default locale to be used by setting the static variable Time::$defaultLocale to a valid locale string. If empty, the default will be taken from the intl.default_locale ini config.

Parameters

string|integer|null $format

Format string.

string|\DateTimeZone|null $timezone

Timezone string or DateTimeZone object in which the date will be displayed. The timezone stored for this object will not be changed.

string|null $locale

The locale name in which the date should be displayed (e.g. pt-BR)

Returns

string|integer —

Formatted and translated date string

__toString()

__toString() : string

Format the instance as a string using the set format

Returns

string

resetToStringFormat()

resetToStringFormat() : void

Reset the format used to the default when type juggling a ChronosInterface instance to a string

setToStringFormat()

setToStringFormat(string  $format) : void

Set the default format used when type juggling a ChronosInterface instance to a string

Parameters

string $format

The format to use in future __toString() calls.

setJsonEncodeFormat()

setJsonEncodeFormat(string|array|integer  $format) : void

Sets the default format used when converting this object to json

Parameters

string|array|integer $format

Format.

parseDateTime()

parseDateTime(string  $time, string|array|null  $format = null) : static|null

Returns a new Time object after parsing the provided time string based on the passed or configured date time format. This method is locale dependent, Any string that is passed to this function will be interpreted as a locale dependent string.

When no $format is provided, the toString format will be used.

If it was impossible to parse the provided time, null will be returned.

Example:

 $time = Time::parseDateTime('10/13/2013 12:54am');
 $time = Time::parseDateTime('13 Oct, 2013 13:54', 'dd MMM, y H:mm');
 $time = Time::parseDateTime('10/10/2015', [IntlDateFormatter::SHORT, -1]);

Parameters

string $time

The time string to parse.

string|array|null $format

Any format accepted by IntlDateFormatter.

Returns

static|null

parseDate()

parseDate(string  $date, string|integer|null  $format = null) : static|null

Returns a new Time object after parsing the provided $date string based on the passed or configured date time format. This method is locale dependent, Any string that is passed to this function will be interpreted as a locale dependent string.

When no $format is provided, the wordFormat format will be used.

If it was impossible to parse the provided time, null will be returned.

Example:

 $time = Time::parseDate('10/13/2013');
 $time = Time::parseDate('13 Oct, 2013', 'dd MMM, y');
 $time = Time::parseDate('13 Oct, 2013', IntlDateFormatter::SHORT);

Parameters

string $date

The date string to parse.

string|integer|null $format

Any format accepted by IntlDateFormatter.

Returns

static|null

parseTime()

parseTime(string  $time, string|integer|null  $format = null) : static|null

Returns a new Time object after parsing the provided $time string based on the passed or configured date time format. This method is locale dependent, Any string that is passed to this function will be interpreted as a locale dependent string.

When no $format is provided, the IntlDateFormatter::SHORT format will be used.

If it was impossible to parse the provided time, null will be returned.

Example:

 $time = Time::parseTime('11:23pm');

Parameters

string $time

The time string to parse.

string|integer|null $format

Any format accepted by IntlDateFormatter.

Returns

static|null

jsonSerialize()

jsonSerialize() : string

Returns a string that should be serialized when converting this object to json

Returns

string

diffFormatter()

diffFormatter(\Cake\Chronos\DifferenceFormatter|null  $formatter = null) : \Cake\Chronos\DifferenceFormatter

Get the difference formatter instance or overwrite the current one.

Parameters

\Cake\Chronos\DifferenceFormatter|null $formatter

The formatter instance when setting.

Returns

\Cake\Chronos\DifferenceFormatter

The formatter instance.

__debugInfo()

__debugInfo() : array

Return properties for debugging.

Returns

array

__construct()

__construct(string|null  $time = null, \DateTimeZone|string|null  $tz = null) 

Create a new MutableDateTime instance.

Please see the testing aids section (specifically static::setTestNow()) for more on the possibility of this constructor returning a test instance.

Parameters

string|null $time

Fixed or relative time

\DateTimeZone|string|null $tz

The timezone for the instance

toImmutable()

toImmutable() : \Cake\Chronos\Chronos

Create a new immutable instance from current mutable instance.

Returns

\Cake\Chronos\Chronos

__set()

__set(string  $name, string|integer|\DateTimeZone  $value) : void

Set a part of the ChronosInterface object

Parameters

string $name

The property to set.

string|integer|\DateTimeZone $value

The value to set.

Throws

\InvalidArgumentException

getWeekendDays()

getWeekendDays() : array

Get weekend days

Returns

array

setWeekendDays()

setWeekendDays(array  $days) : void

Set weekend days

Parameters

array $days

Which days are 'weekends'.

eq()

eq(\Cake\Chronos\ChronosInterface  $dt) : boolean

Determines if the instance is equal to another

Parameters

\Cake\Chronos\ChronosInterface $dt

The instance to compare with.

Returns

boolean

ne()

ne(\Cake\Chronos\ChronosInterface  $dt) : boolean

Determines if the instance is not equal to another

Parameters

\Cake\Chronos\ChronosInterface $dt

The instance to compare with.

Returns

boolean

gt()

gt(\Cake\Chronos\ChronosInterface  $dt) : boolean

Determines if the instance is greater (after) than another

Parameters

\Cake\Chronos\ChronosInterface $dt

The instance to compare with.

Returns

boolean

gte()

gte(\Cake\Chronos\ChronosInterface  $dt) : boolean

Determines if the instance is greater (after) than or equal to another

Parameters

\Cake\Chronos\ChronosInterface $dt

The instance to compare with.

Returns

boolean

lt()

lt(\Cake\Chronos\ChronosInterface  $dt) : boolean

Determines if the instance is less (before) than another

Parameters

\Cake\Chronos\ChronosInterface $dt

The instance to compare with.

Returns

boolean

lte()

lte(\Cake\Chronos\ChronosInterface  $dt) : boolean

Determines if the instance is less (before) or equal to another

Parameters

\Cake\Chronos\ChronosInterface $dt

The instance to compare with.

Returns

boolean

between()

between(\Cake\Chronos\ChronosInterface  $dt1, \Cake\Chronos\ChronosInterface  $dt2, boolean  $equal = true) : boolean

Determines if the instance is between two others

Parameters

\Cake\Chronos\ChronosInterface $dt1

The instance to compare with.

\Cake\Chronos\ChronosInterface $dt2

The instance to compare with.

boolean $equal

Indicates if a > and < comparison should be used or <= or >=

Returns

boolean

min()

min(\Cake\Chronos\ChronosInterface|null  $dt = null) : static

Get the minimum instance between a given instance (default now) and the current instance.

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to compare with.

Returns

static

max()

max(\Cake\Chronos\ChronosInterface|null  $dt = null) : static

Get the maximum instance between a given instance (default now) and the current instance.

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to compare with.

Returns

static

isWeekday()

isWeekday() : boolean

Determines if the instance is a weekday

Returns

boolean

isWeekend()

isWeekend() : boolean

Determines if the instance is a weekend day

Returns

boolean

isYesterday()

isYesterday() : boolean

Determines if the instance is yesterday

Returns

boolean

isToday()

isToday() : boolean

Determines if the instance is today

Returns

boolean

isTomorrow()

isTomorrow() : boolean

Determines if the instance is tomorrow

Returns

boolean

isNextWeek()

isNextWeek() : boolean

Determines if the instance is within the next week

Returns

boolean

isLastWeek()

isLastWeek() : boolean

Determines if the instance is within the last week

Returns

boolean

isNextMonth()

isNextMonth() : boolean

Determines if the instance is within the next month

Returns

boolean

isLastMonth()

isLastMonth() : boolean

Determines if the instance is within the last month

Returns

boolean

isNextYear()

isNextYear() : boolean

Determines if the instance is within the next year

Returns

boolean

isLastYear()

isLastYear() : boolean

Determines if the instance is within the last year

Returns

boolean

isFuture()

isFuture() : boolean

Determines if the instance is in the future, ie. greater (after) than now

Returns

boolean

isPast()

isPast() : boolean

Determines if the instance is in the past, ie. less (before) than now

Returns

boolean

isLeapYear()

isLeapYear() : boolean

Determines if the instance is a leap year

Returns

boolean

isSameDay()

isSameDay(\Cake\Chronos\ChronosInterface  $dt) : boolean

Checks if the passed in date is the same day as the instance current day.

Parameters

\Cake\Chronos\ChronosInterface $dt

The instance to check against.

Returns

boolean

isSunday()

isSunday() : boolean

Checks if this day is a Sunday.

Returns

boolean

isMonday()

isMonday() : boolean

Checks if this day is a Monday.

Returns

boolean

isTuesday()

isTuesday() : boolean

Checks if this day is a Tuesday.

Returns

boolean

isWednesday()

isWednesday() : boolean

Checks if this day is a Wednesday.

Returns

boolean

isThursday()

isThursday() : boolean

Checks if this day is a Thursday.

Returns

boolean

isFriday()

isFriday() : boolean

Checks if this day is a Friday.

Returns

boolean

isSaturday()

isSaturday() : boolean

Checks if this day is a Saturday.

Returns

boolean

isThisWeek()

isThisWeek() : boolean

Returns true if this object represents a date within the current week

Returns

boolean

isThisMonth()

isThisMonth() : boolean

Returns true if this object represents a date within the current month

Returns

boolean

isThisYear()

isThisYear() : boolean

Returns true if this object represents a date within the current year

Returns

boolean

isBirthday()

isBirthday(\Cake\Chronos\ChronosInterface|null  $dt = null) : static

Check if its the birthday. Compares the date/month values of the two dates.

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to compare with or null to use current day.

Returns

static

wasWithinLast()

wasWithinLast(string|integer  $timeInterval) : boolean

Returns true this instance will happen within the specified interval

This overridden method provides backwards compatible behavior for integers, or strings with trailing spaces. This behavior is deprecated and will be removed in future versions of CakePHP.

Parameters

string|integer $timeInterval

the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.

Returns

boolean

isWithinNext()

isWithinNext(string|integer  $timeInterval) : boolean

Returns true this instance happened within the specified interval

This overridden method provides backwards compatible behavior for integers, or strings with trailing spaces. This behavior is deprecated and will be removed in future versions of CakePHP.

Parameters

string|integer $timeInterval

the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.

Returns

boolean

isMutable()

isMutable() : boolean

Check if instance of ChronosInterface is mutable.

Returns

boolean

diffInYears()

diffInYears(\Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in years

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffInMonths()

diffInMonths(\Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in months

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffInWeeks()

diffInWeeks(\Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in weeks

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffInDays()

diffInDays(\Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in days

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffInDaysFiltered()

diffInDaysFiltered(callable  $callback, \Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in days using a filter callable

Parameters

callable $callback

The callback to use for filtering.

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffInHoursFiltered()

diffInHoursFiltered(callable  $callback, \Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in hours using a filter callable

Parameters

callable $callback

The callback to use for filtering.

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffFiltered()

diffFiltered(\Cake\Chronos\ChronosInterval  $ci, callable  $callback, \Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference by the given interval using a filter callable

Parameters

\Cake\Chronos\ChronosInterval $ci

An interval to traverse by

callable $callback

The callback to use for filtering.

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffInWeekdays()

diffInWeekdays(\Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in weekdays

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffInWeekendDays()

diffInWeekendDays(\Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in weekend days using a filter

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffInHours()

diffInHours(\Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in hours

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffInMinutes()

diffInMinutes(\Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in minutes

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

diffInSeconds()

diffInSeconds(\Cake\Chronos\ChronosInterface|null  $dt = null, boolean  $abs = true) : integer

Get the difference in seconds

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to difference from.

boolean $abs

Get the absolute of the difference

Returns

integer

secondsSinceMidnight()

secondsSinceMidnight() : integer

The number of seconds since midnight.

Returns

integer

secondsUntilEndOfDay()

secondsUntilEndOfDay() : integer

The number of seconds until 23:59:59.

Returns

integer

fromNow()

fromNow(\DateTime|\DateTimeImmutable  $datetime) : \DateInterval|boolean

Convenience method for getting the remaining time from a given time.

Parameters

\DateTime|\DateTimeImmutable $datetime

The date to get the remaining time from.

Returns

\DateInterval|boolean —

The DateInterval object representing the difference between the two dates or FALSE on failure.

diffForHumans()

diffForHumans(\Cake\Chronos\ChronosInterface|null  $other = null, boolean  $absolute = false) : string

Get the difference in a human readable format.

When comparing a value in the past to default now: 1 hour ago 5 months ago

When comparing a value in the future to default now: 1 hour from now 5 months from now

When comparing a value in the past to another value: 1 hour before 5 months before

When comparing a value in the future to another value: 1 hour after 5 months after

Parameters

\Cake\Chronos\ChronosInterface|null $other

The datetime to compare with.

boolean $absolute

removes time difference modifiers ago, after, etc

Returns

string

instance()

instance(\DateTimeInterface  $dt) : static

Create a ChronosInterface instance from a DateTimeInterface one

Parameters

\DateTimeInterface $dt

The datetime instance to convert.

Returns

static

parse()

parse(string  $time = 'now', \DateTimeZone|string|null  $tz = null) : static

Create a ChronosInterface instance from a string. This is an alias for the constructor that allows better fluent syntax as it allows you to do ChronosInterface::parse('Monday next week')->fn() rather than (new Chronos('Monday next week'))->fn()

Parameters

string $time

The strtotime compatible string to parse

\DateTimeZone|string|null $tz

The DateTimeZone object or timezone name.

Returns

static

now()

now(\DateTimeZone|string|null  $tz = null) : static

Get a ChronosInterface instance for the current date and time

Parameters

\DateTimeZone|string|null $tz

The DateTimeZone object or timezone name.

Returns

static

today()

today(\DateTimeZone|string|null  $tz = null) : static

Create a ChronosInterface instance for today

Parameters

\DateTimeZone|string|null $tz

The timezone to use.

Returns

static

tomorrow()

tomorrow(\DateTimeZone|string|null  $tz = null) : static

Create a ChronosInterface instance for tomorrow

Parameters

\DateTimeZone|string|null $tz

The DateTimeZone object or timezone name the new instance should use.

Returns

static

yesterday()

yesterday(\DateTimeZone|string|null  $tz = null) : static

Create a ChronosInterface instance for yesterday

Parameters

\DateTimeZone|string|null $tz

The DateTimeZone object or timezone name the new instance should use.

Returns

static

maxValue()

maxValue() : \Cake\Chronos\ChronosInterface

Create a ChronosInterface instance for the greatest supported date.

Returns

\Cake\Chronos\ChronosInterface

minValue()

minValue() : \Cake\Chronos\ChronosInterface

Create a ChronosInterface instance for the lowest supported date.

Returns

\Cake\Chronos\ChronosInterface

create()

create(integer|null  $year = null, integer|null  $month = null, integer|null  $day = null, integer|null  $hour = null, integer|null  $minute = null, integer|null  $second = null, \DateTimeZone|string|null  $tz = null) : static

Create a new ChronosInterface instance from a specific date and time.

If any of $year, $month or $day are set to null their now() values will be used.

If $hour is null it will be set to its now() value and the default values for $minute and $second will be their now() values. If $hour is not null then the default values for $minute and $second will be 0.

Parameters

integer|null $year

The year to create an instance with.

integer|null $month

The month to create an instance with.

integer|null $day

The day to create an instance with.

integer|null $hour

The hour to create an instance with.

integer|null $minute

The minute to create an instance with.

integer|null $second

The second to create an instance with.

\DateTimeZone|string|null $tz

The DateTimeZone object or timezone name the new instance should use.

Returns

static

createFromDate()

createFromDate(integer  $year = null, integer  $month = null, integer  $day = null, \DateTimeZone|string|null  $tz = null) : static

Create a ChronosInterface instance from just a date. The time portion is set to now.

Parameters

integer $year

The year to create an instance with.

integer $month

The month to create an instance with.

integer $day

The day to create an instance with.

\DateTimeZone|string|null $tz

The DateTimeZone object or timezone name the new instance should use.

Returns

static

createFromTime()

createFromTime(integer|null  $hour = null, integer|null  $minute = null, integer|null  $second = null, \DateTimeZone|string|null  $tz = null) : static

Create a ChronosInterface instance from just a time. The date portion is set to today.

Parameters

integer|null $hour

The hour to create an instance with.

integer|null $minute

The minute to create an instance with.

integer|null $second

The second to create an instance with.

\DateTimeZone|string|null $tz

The DateTimeZone object or timezone name the new instance should use.

Returns

static

createFromFormat()

createFromFormat(string  $format, string  $time, \DateTimeZone|string|null  $tz = null) : static

Create a ChronosInterface instance from a specific format

Parameters

string $format

The date() compatible format string.

string $time

The formatted date string to interpret.

\DateTimeZone|string|null $tz

The DateTimeZone object or timezone name the new instance should use.

Throws

\InvalidArgumentException

Returns

static

createFromTimestamp()

createFromTimestamp(integer  $timestamp, \DateTimeZone|string|null  $tz = null) : static

Create a ChronosInterface instance from a timestamp

Parameters

integer $timestamp

The timestamp to create an instance from.

\DateTimeZone|string|null $tz

The DateTimeZone object or timezone name the new instance should use.

Returns

static

createFromTimestampUTC()

createFromTimestampUTC(integer  $timestamp) : static

Create a ChronosInterface instance from an UTC timestamp

Parameters

integer $timestamp

The UTC timestamp to create an instance from.

Returns

static

getLastErrors()

getLastErrors() : array

Returns any errors or warnings that were found during the parsing of the last object created by this class.

Returns

array

toDateString()

toDateString() : string

Format the instance as date

Returns

string

toFormattedDateString()

toFormattedDateString() : string

Format the instance as a readable date

Returns

string

toTimeString()

toTimeString() : string

Format the instance as time

Returns

string

toDateTimeString()

toDateTimeString() : string

Format the instance as date and time

Returns

string

toDayDateTimeString()

toDayDateTimeString() : string

Format the instance with day, date and time

Returns

string

toAtomString()

toAtomString() : string

Format the instance as ATOM

Returns

string

toCookieString()

toCookieString() : string

Format the instance as COOKIE

Returns

string

toIso8601String()

toIso8601String() : string

Format the instance as ISO8601

Returns

string

toRfc822String()

toRfc822String() : string

Format the instance as RFC822

Returns

string

toRfc850String()

toRfc850String() : string

Format the instance as RFC850

Returns

string

toRfc1036String()

toRfc1036String() : string

Format the instance as RFC1036

Returns

string

toRfc1123String()

toRfc1123String() : string

Format the instance as RFC1123

Returns

string

toRfc2822String()

toRfc2822String() : string

Format the instance as RFC2822

Returns

string

toRfc3339String()

toRfc3339String() : string

Format the instance as RFC3339

Returns

string

toRssString()

toRssString() : string

Format the instance as RSS

Returns

string

toW3cString()

toW3cString() : string

Format the instance as W3C

Returns

string

toUnixString()

toUnixString() : string

Returns a UNIX timestamp.

Returns

string —

UNIX timestamp

toQuarter()

toQuarter(boolean  $range = false) : integer|array

Returns the quarter

Parameters

boolean $range

Range.

Returns

integer|array —

1, 2, 3, or 4 quarter of year, or array if $range true

toWeek()

toWeek() : integer

Returns

integer

__get()

__get(string  $name) : string|integer|\DateTimeZone

Get a part of the ChronosInterface object

Parameters

string $name

The property name to read.

Throws

\InvalidArgumentException

Returns

string|integer|\DateTimeZone —

The property value.

__isset()

__isset(string  $name) : boolean

Check if an attribute exists on the object

Parameters

string $name

The property name to check.

Returns

boolean —

Whether or not the property exists.

getWeekStartsAt()

getWeekStartsAt() : integer

Get the first day of week

Returns

integer

setWeekStartsAt()

setWeekStartsAt(integer  $day) : void

Set the first day of week

Parameters

integer $day

The day the week starts with.

getWeekEndsAt()

getWeekEndsAt() : integer

Get the last day of week

Returns

integer

setWeekEndsAt()

setWeekEndsAt(integer  $day) : void

Set the last day of week

Parameters

integer $day

The day the week ends with.

setDate()

setDate(integer  $year, integer  $month, integer  $day) : static

Set the date to a different date.

Workaround for a PHP bug related to the first day of a month

Parameters

integer $year

The year to set.

integer $month

The month to set.

integer $day

The day to set.

Returns

static

setDateTime()

setDateTime(integer  $year, integer  $month, integer  $day, integer  $hour, integer  $minute, integer  $second) : static

Set the date and time all together

Parameters

integer $year

The year to set.

integer $month

The month to set.

integer $day

The day to set.

integer $hour

The hour to set.

integer $minute

The minute to set.

integer $second

The second to set.

Returns

static

setTimeFromTimeString()

setTimeFromTimeString(string  $time) : static

Set the time by time string

Parameters

string $time

Time as string.

Returns

static

timestamp()

timestamp(integer  $value) : static

Set the instance's timestamp

Parameters

integer $value

The timestamp value to set.

Returns

static

year()

year(integer  $value) : static

Set the instance's year

Parameters

integer $value

The year value.

Returns

static

month()

month(integer  $value) : static

Set the instance's month

Parameters

integer $value

The month value.

Returns

static

day()

day(integer  $value) : static

Set the instance's day

Parameters

integer $value

The day value.

Returns

static

hour()

hour(integer  $value) : static

Set the instance's hour

Parameters

integer $value

The hour value.

Returns

static

minute()

minute(integer  $value) : static

Set the instance's minute

Parameters

integer $value

The minute value.

Returns

static

second()

second(integer  $value) : static

Set the instance's second

Parameters

integer $value

The seconds value.

Returns

static

addYears()

addYears(integer  $value) : static

Add years to the instance. Positive $value travel forward while negative $value travel into the past.

Parameters

integer $value

The number of years to add.

Returns

static

addYear()

addYear(integer  $value = 1) : static

Add a year to the instance

Parameters

integer $value

The number of years to add.

Returns

static

subYear()

subYear(integer  $value = 1) : static

Remove a year from the instance

Parameters

integer $value

The number of years to remove.

Returns

static

subYears()

subYears(integer  $value) : static

Remove years from the instance.

Parameters

integer $value

The number of years to remove.

Returns

static

addMonths()

addMonths(integer  $value) : static

Add months to the instance. Positive $value travels forward while negative $value travels into the past.

When adding or subtracting months, if the resulting time is a date that does not exist, the result of this operation will always be the last day of the intended month.

Example:

 (new Chronos('2015-01-03'))->addMonths(1); // Results in 2015-02-03

 (new Chronos('2015-01-31'))->addMonths(1); // Results in 2015-02-28

Parameters

integer $value

The number of months to add.

Returns

static

addMonth()

addMonth(integer  $value = 1) : static

Add a month to the instance

When adding or subtracting months, if the resulting time is a date that does not exist, the result of this operation will always be the last day of the intended month.

Example:

 (new Chronos('2015-01-03'))->addMonth(); // Results in 2015-02-03

 (new Chronos('2015-01-31'))->addMonth(); // Results in 2015-02-28

Parameters

integer $value

The number of months to add.

Returns

static

subMonth()

subMonth(integer  $value = 1) : static

Remove a month from the instance

When adding or subtracting months, if the resulting time is a date that does not exist, the result of this operation will always be the last day of the intended month.

Example:

 (new Chronos('2015-03-01'))->subMonth(); // Results in 2015-02-01

 (new Chronos('2015-03-31'))->subMonth(); // Results in 2015-02-28

Parameters

integer $value

The number of months to remove.

Returns

static

subMonths()

subMonths(integer  $value) : static

Remove months from the instance

When adding or subtracting months, if the resulting time is a date that does not exist, the result of this operation will always be the last day of the intended month.

Example:

 (new Chronos('2015-03-01'))->subMonths(1); // Results in 2015-02-01

 (new Chronos('2015-03-31'))->subMonths(1); // Results in 2015-02-28

Parameters

integer $value

The number of months to remove.

Returns

static

addMonthsWithOverflow()

addMonthsWithOverflow(integer  $value) : static

Add months with overflowing to the instance. Positive $value travels forward while negative $value travels into the past.

Parameters

integer $value

The number of months to add.

Returns

static

addMonthWithOverflow()

addMonthWithOverflow(integer  $value = 1) : static

Add a month with overflow to the instance

Parameters

integer $value

The number of months to add.

Returns

static

subMonthWithOverflow()

subMonthWithOverflow(integer  $value = 1) : static

Remove a month with overflow from the instance

Parameters

integer $value

The number of months to remove.

Returns

static

subMonthsWithOverflow()

subMonthsWithOverflow(integer  $value) : static

Remove months with overflow from the instance

Parameters

integer $value

The number of months to remove.

Returns

static

addDays()

addDays(integer  $value) : static

Add days to the instance. Positive $value travels forward while negative $value travels into the past.

Parameters

integer $value

The number of days to add.

Returns

static

addDay()

addDay(integer  $value = 1) : static

Add a day to the instance

Parameters

integer $value

The number of days to add.

Returns

static

subDay()

subDay(integer  $value = 1) : static

Remove a day from the instance

Parameters

integer $value

The number of days to remove.

Returns

static

subDays()

subDays(integer  $value) : static

Remove days from the instance

Parameters

integer $value

The number of days to remove.

Returns

static

addWeekdays()

addWeekdays(integer  $value) : static

Add weekdays to the instance. Positive $value travels forward while negative $value travels into the past.

Parameters

integer $value

The number of weekdays to add.

Returns

static

addWeekday()

addWeekday(integer  $value = 1) : static

Add a weekday to the instance

Parameters

integer $value

The number of weekdays to add.

Returns

static

subWeekdays()

subWeekdays(integer  $value) : static

Remove weekdays from the instance

Parameters

integer $value

The number of weekdays to remove.

Returns

static

subWeekday()

subWeekday(integer  $value = 1) : static

Remove a weekday from the instance

Parameters

integer $value

The number of weekdays to remove.

Returns

static

addWeeks()

addWeeks(integer  $value) : static

Add weeks to the instance. Positive $value travels forward while negative $value travels into the past.

Parameters

integer $value

The number of weeks to add.

Returns

static

addWeek()

addWeek(integer  $value = 1) : static

Add a week to the instance

Parameters

integer $value

The number of weeks to add.

Returns

static

subWeek()

subWeek(integer  $value = 1) : static

Remove a week from the instance

Parameters

integer $value

The number of weeks to remove.

Returns

static

subWeeks()

subWeeks(integer  $value) : static

Remove weeks to the instance

Parameters

integer $value

The number of weeks to remove.

Returns

static

addHours()

addHours(integer  $value) : static

Add hours to the instance. Positive $value travels forward while negative $value travels into the past.

Parameters

integer $value

The number of hours to add.

Returns

static

addHour()

addHour(integer  $value = 1) : static

Add an hour to the instance

Parameters

integer $value

The number of hours to add.

Returns

static

subHour()

subHour(integer  $value = 1) : static

Remove an hour from the instance

Parameters

integer $value

The number of hours to remove.

Returns

static

subHours()

subHours(integer  $value) : static

Remove hours from the instance

Parameters

integer $value

The number of hours to remove.

Returns

static

addMinutes()

addMinutes(integer  $value) : static

Add minutes to the instance. Positive $value travels forward while negative $value travels into the past.

Parameters

integer $value

The number of minutes to add.

Returns

static

addMinute()

addMinute(integer  $value = 1) : static

Add a minute to the instance

Parameters

integer $value

The number of minutes to add.

Returns

static

subMinute()

subMinute(integer  $value = 1) : static

Remove a minute from the instance

Parameters

integer $value

The number of minutes to remove.

Returns

static

subMinutes()

subMinutes(integer  $value) : static

Remove minutes from the instance

Parameters

integer $value

The number of minutes to remove.

Returns

static

addSeconds()

addSeconds(integer  $value) : static

Add seconds to the instance. Positive $value travels forward while negative $value travels into the past.

Parameters

integer $value

The number of seconds to add.

Returns

static

addSecond()

addSecond(integer  $value = 1) : static

Add a second to the instance

Parameters

integer $value

The number of seconds to add.

Returns

static

subSecond()

subSecond(integer  $value = 1) : static

Remove a second from the instance

Parameters

integer $value

The number of seconds to remove.

Returns

static

subSeconds()

subSeconds(integer  $value) : static

Remove seconds from the instance

Parameters

integer $value

The number of seconds to remove.

Returns

static

startOfDay()

startOfDay() : static

Resets the time to 00:00:00

Returns

static

endOfDay()

endOfDay() : static

Resets the time to 23:59:59

Returns

static

startOfMonth()

startOfMonth() : static

Resets the date to the first day of the month and the time to 00:00:00

Returns

static

endOfMonth()

endOfMonth() : static

Resets the date to end of the month and time to 23:59:59

Returns

static

startOfYear()

startOfYear() : static

Resets the date to the first day of the year and the time to 00:00:00

Returns

static

endOfYear()

endOfYear() : static

Resets the date to end of the year and time to 23:59:59

Returns

static

startOfDecade()

startOfDecade() : static

Resets the date to the first day of the decade and the time to 00:00:00

Returns

static

endOfDecade()

endOfDecade() : static

Resets the date to end of the decade and time to 23:59:59

Returns

static

startOfCentury()

startOfCentury() : static

Resets the date to the first day of the century and the time to 00:00:00

Returns

static

endOfCentury()

endOfCentury() : static

Resets the date to end of the century and time to 23:59:59

Returns

static

startOfWeek()

startOfWeek() : static

Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00

Returns

static

endOfWeek()

endOfWeek() : static

Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59

Returns

static

next()

next(integer|null  $dayOfWeek = null) : mixed

Modify to the next occurrence of a given day of the week.

If no dayOfWeek is provided, modify to the next occurrence of the current day of the week. Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer|null $dayOfWeek

The day of the week to move to.

Returns

mixed

previous()

previous(integer|null  $dayOfWeek = null) : mixed

Modify to the previous occurrence of a given day of the week.

If no dayOfWeek is provided, modify to the previous occurrence of the current day of the week. Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer|null $dayOfWeek

The day of the week to move to.

Returns

mixed

firstOfMonth()

firstOfMonth(integer|null  $dayOfWeek = null) : mixed

Modify to the first occurrence of a given day of the week in the current month. If no dayOfWeek is provided, modify to the first day of the current month. Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer|null $dayOfWeek

The day of the week to move to.

Returns

mixed

lastOfMonth()

lastOfMonth(integer|null  $dayOfWeek = null) : mixed

Modify to the last occurrence of a given day of the week in the current month. If no dayOfWeek is provided, modify to the last day of the current month. Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer|null $dayOfWeek

The day of the week to move to.

Returns

mixed

nthOfMonth()

nthOfMonth(integer  $nth, integer  $dayOfWeek) : mixed

Modify to the given occurrence of a given day of the week in the current month. If the calculated occurrence is outside the scope of the current month, then return false and no modifications are made.

Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer $nth

The offset to use.

integer $dayOfWeek

The day of the week to move to.

Returns

mixed

firstOfQuarter()

firstOfQuarter(integer|null  $dayOfWeek = null) : mixed

Modify to the first occurrence of a given day of the week in the current quarter. If no dayOfWeek is provided, modify to the first day of the current quarter. Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer|null $dayOfWeek

The day of the week to move to.

Returns

mixed

lastOfQuarter()

lastOfQuarter(integer|null  $dayOfWeek = null) : mixed

Modify to the last occurrence of a given day of the week in the current quarter. If no dayOfWeek is provided, modify to the last day of the current quarter. Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer|null $dayOfWeek

The day of the week to move to.

Returns

mixed

nthOfQuarter()

nthOfQuarter(integer  $nth, integer  $dayOfWeek) : mixed

Modify to the given occurrence of a given day of the week in the current quarter. If the calculated occurrence is outside the scope of the current quarter, then return false and no modifications are made.

Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer $nth

The offset to use.

integer $dayOfWeek

The day of the week to move to.

Returns

mixed

firstOfYear()

firstOfYear(integer|null  $dayOfWeek = null) : mixed

Modify to the first occurrence of a given day of the week in the current year. If no dayOfWeek is provided, modify to the first day of the current year. Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer|null $dayOfWeek

The day of the week to move to.

Returns

mixed

lastOfYear()

lastOfYear(integer|null  $dayOfWeek = null) : mixed

Modify to the last occurrence of a given day of the week in the current year. If no dayOfWeek is provided, modify to the last day of the current year. Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer|null $dayOfWeek

The day of the week to move to.

Returns

mixed

nthOfYear()

nthOfYear(integer  $nth, integer  $dayOfWeek) : mixed

Modify to the given occurrence of a given day of the week in the current year. If the calculated occurrence is outside the scope of the current year, then return false and no modifications are made.

Use the supplied consts to indicate the desired dayOfWeek, ex. ChronosInterface::MONDAY.

Parameters

integer $nth

The offset to use.

integer $dayOfWeek

The day of the week to move to.

Returns

mixed

average()

average(\Cake\Chronos\ChronosInterface|null  $dt = null) : static

Modify the current instance to the average of a given instance (default now) and the current instance.

Parameters

\Cake\Chronos\ChronosInterface|null $dt

The instance to compare with.

Returns

static

hasRelativeKeywords()

hasRelativeKeywords(string  $time) : boolean

Determine if there is a relative keyword in the time string, this is to create dates relative to now for test instances. e.g.: next tuesday

Parameters

string $time

The time string to check.

Returns

boolean —

true if there is a keyword, otherwise false

setTestNow()

setTestNow(\Cake\Chronos\ChronosInterface|string|null  $testNow = null) : void

Set the test now used by Date and Time classes provided by Chronos

Parameters

\Cake\Chronos\ChronosInterface|string|null $testNow

The instance to use for all future instances.

getTestNow()

getTestNow() : static|null

Get the test instance stored in Chronos

Returns

static|null —

the current instance used for testing or null.

hasTestNow()

hasTestNow() : boolean

Get whether or not Chronos has a test instance set.

Returns

boolean —

True if there is a test instance, otherwise false

timezone()

timezone(\DateTimeZone|string  $value) : static

Alias for setTimezone()

Parameters

\DateTimeZone|string $value

The DateTimeZone object or timezone name to use.

Returns

static

tz()

tz(\DateTimeZone|string  $value) : static

Alias for setTimezone()

Parameters

\DateTimeZone|string $value

The DateTimeZone object or timezone name to use.

Returns

static

setTimezone()

setTimezone(\DateTimeZone|string  $value) : static

Set the instance's timezone from a string or object

Parameters

\DateTimeZone|string $value

The DateTimeZone object or timezone name to use.

Returns

static

timeAgoInWords()

timeAgoInWords(array  $options = array()) : string

Returns either a relative or a formatted absolute date depending on the difference between the current time and this object.

Options:

  • from => another Time object representing the "now" time
  • format => a fall back format if the relative time is longer than the duration specified by end
  • accuracy => Specifies how accurate the date should be described (array)
    • year => The format if years > 0 (default "day")
    • month => The format if months > 0 (default "day")
    • week => The format if weeks > 0 (default "day")
    • day => The format if weeks > 0 (default "hour")
    • hour => The format if hours > 0 (default "minute")
    • minute => The format if minutes > 0 (default "minute")
    • second => The format if seconds > 0 (default "second")
  • end => The end of relative time telling
  • relativeString => The printf compatible string when outputting relative time
  • absoluteString => The printf compatible string when outputting absolute time
  • timezone => The user timezone the timestamp should be formatted in.

Relative dates look something like this:

  • 3 weeks, 4 days ago
  • 15 seconds ago

Default date formatting is d/M/YY e.g: on 18/2/09. Formatting is done internally using i18nFormat, see the method for the valid formatting strings

The returned string includes 'ago' or 'on' and assumes you'll properly add a word like 'Posted ' before the function output.

NOTE: If the difference is one week or more, the lowest level of accuracy is day

Parameters

array $options

Array of options.

Returns

string —

Relative time string.

listTimezones()

listTimezones(integer|string|null  $filter = null, string|null  $country = null, boolean|array  $options = array()) : array

Get list of timezone identifiers

Parameters

integer|string|null $filter

A regex to filter identifier Or one of DateTimeZone class constants

string|null $country

A two-letter ISO 3166-1 compatible country code. This option is only used when $filter is set to DateTimeZone::PER_COUNTRY

boolean|array $options

If true (default value) groups the identifiers list by primary region. Otherwise, an array containing group, abbr, before, and after keys. Setting group and abbr to true will group results and append timezone abbreviation in the display value. Set before and after to customize the abbreviation wrapper.

Returns

array —

List of timezone identifiers

_formatObject()

_formatObject(\DateTime  $date, string|integer|array  $format, string  $locale) : string

Returns a translated and localized date string.

Implements what IntlDateFormatter::formatObject() is in PHP 5.5+

Parameters

\DateTime $date

Date.

string|integer|array $format

Format.

string $locale

The locale name in which the date should be displayed.

Returns

string

safeCreateDateTimeZone()

safeCreateDateTimeZone(\DateTimeZone|string|null  $object) : \DateTimeZone

Creates a DateTimeZone from a string or a DateTimeZone

Parameters

\DateTimeZone|string|null $object

The value to convert.

Throws

\InvalidArgumentException

Returns

\DateTimeZone

setDateParent()

setDateParent(integer  $year, integer  $month, integer  $day) : static

Just calling to parent setDate It used in overwritten setDate

Parameters

integer $year

The year to set.

integer $month

The month to set.

integer $day

The day to set.

Returns

static