Fr3nch13/CakePHP Utilities

FrozenTime extends Chronos
in package
implements I18nDateTimeInterface uses DateFormatTrait

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

This object provides an immutable variant of

Table of Contents

Interfaces

I18nDateTimeInterface
Interface for date formatting methods shared by both Time & Date.

Constants

UNIX_TIMESTAMP_FORMAT  = 'unixTimestampFormat'
serialise the value as a Unix Timestamp

Properties

$age  : int
$day  : int
$dayOfWeek  : int
$dayOfYear  : int
$daysInMonth  : int
$dst  : bool
$hour  : int
$local  : bool
$micro  : int
$microsecond  : int
$minute  : int
$month  : int
$niceFormat  : array<string|int, int>|string|int
The format to use when formatting a time using `Cake\I18n\FrozenTime::nice()`
$offset  : int
$offsetHours  : int
$quarter  : int
$second  : int
$timestamp  : int
$timezone  : DateTimeZone
$timezoneName  : string
$tz  : DateTimeZone
$tzName  : string
$utc  : bool
$weekOfMonth  : int
$weekOfYear  : int
$wordAccuracy  : array<string|int, string>
The format to use when formatting a time using `Time::timeAgoInWords()` and the difference is less than `Time::$wordEnd`
$wordEnd  : string
The end of relative time telling
$wordFormat  : array<string|int, int>|string|int
The format to use when formatting a time using `Cake\I18n\FrozenTime::timeAgoInWords()` and the difference is more than `Cake\I18n\FrozenTime::$wordEnd`
$year  : int
$yearIso  : int
$_formatters  : array<string|int, IntlDateFormatter>
In-memory cache of date formatters
$_jsonEncodeFormat  : Closure|array<string|int, int>|string|int
The format to use when converting this object to JSON.
$_lastErrors  : array<string|int, mixed>
Holds the last error generated by createFromFormat
$_toStringFormat  : array<string|int, int>|string|int
The format to use when formatting a time using `Cake\I18n\FrozenTime::i18nFormat()` and `__toString`. This format is also used by `parseDateTime()`.
$days  : array<string|int, mixed>
Names of days of the week.
$defaultLocale  : string|null
The default locale to be used for displaying formatted date strings.
$diffFormatter  : DifferenceFormatterInterface|null
Instance of the diff formatting object.
$lenientParsing  : bool
Whether lenient parsing is enabled for IntlDateFormatter.
$relativePattern  : string
Regex for relative period.
$testNow  : ChronosInterface|null
A test ChronosInterface instance to be returned when now instances are created
$toStringFormat  : string
Format to use for __toString method when type juggling occurs.
$weekendDays  : array<string|int, mixed>
Days of weekend
$weekEndsAt  : int
Last day of week
$weekStartsAt  : int
First day of week

Methods

__construct()  : mixed
Create a new immutable time instance.
__debugInfo()  : array<string, mixed>
Returns the data that should be displayed when debugging this object
__get()  : string|int|bool|DateTimeZone
Get a part of the ChronosInterface object
__isset()  : bool
Check if an attribute exists on the object
__toString()  : string
addDay()  : static
Add a day to the instance
addDays()  : static
Add days to the instance. Positive $value travels forward while negative $value travels into the past.
addHour()  : static
Add an hour to the instance
addHours()  : static
Add hours to the instance. Positive $value travels forward while negative $value travels into the past.
addMinute()  : static
Add a minute to the instance
addMinutes()  : static
Add minutes to the instance. Positive $value travels forward while negative $value travels into the past.
addMonth()  : static
Add a month to the instance
addMonths()  : static
Add months to the instance. Positive $value travels forward while negative $value travels into the past.
addMonthsWithOverflow()  : static
Add months with overflowing to the instance. Positive $value travels forward while negative $value travels into the past.
addMonthWithOverflow()  : static
Add a month with overflow to the instance.
addSecond()  : static
Add a second to the instance
addSeconds()  : static
Add seconds to the instance. Positive $value travels forward while negative $value travels into the past.
addWeek()  : static
Add a week to the instance
addWeekday()  : static
Add a weekday to the instance
addWeekdays()  : static
Add weekdays to the instance. Positive $value travels forward while negative $value travels into the past.
addWeeks()  : static
Add weeks to the instance. Positive $value travels forward while negative $value travels into the past.
addYear()  : static
Add a year to the instance.
addYears()  : static
Add years to the instance. Positive $value travel forward while negative $value travel into the past.
addYearsWithOverflow()  : static
Add years with overflowing to the instance. Positive $value travels forward while negative $value travels into the past.
addYearWithOverflow()  : static
Add a year with overflow to the instance.
average()  : static
Modify the current instance to the average of a given instance (default now) and the current instance.
between()  : bool
Determines if the instance is between two others
closest()  : ChronosInterface
Get the closest date from the instance.
copy()  : static
Get a copy of the instance
create()  : static
Create a new ChronosInterface instance from a specific date and time.
createFromArray()  : static
Creates a ChronosInterface instance from an array of date and time values.
createFromDate()  : static
Create a ChronosInterface instance from just a date. The time portion is set to now.
createFromFormat()  : static
Create a ChronosInterface instance from a specific format
createFromTime()  : static
Create a ChronosInterface instance from just a time. The date portion is set to today.
createFromTimestamp()  : static
Create a ChronosInterface instance from a timestamp
createFromTimestampUTC()  : static
Create a ChronosInterface instance from an UTC timestamp
createInterval()  : DateInterval
Create a new DateInterval instance from specified values.
day()  : static
Set the instance's day
diffFiltered()  : int
Get the difference by the given interval using a filter callable
diffForHumans()  : string
Get the difference in a human readable format.
diffFormatter()  : DifferenceFormatterInterface
Get the difference formatter instance or overwrite the current one.
diffInDays()  : int
Get the difference in days
diffInDaysFiltered()  : int
Get the difference in days using a filter callable
diffInHours()  : int
Get the difference in hours
diffInHoursFiltered()  : int
Get the difference in hours using a filter callable
diffInMinutes()  : int
Get the difference in minutes
diffInMonths()  : int
Get the difference in months
diffInMonthsIgnoreTimezone()  : int
Get the difference in months ignoring the timezone. This means the months are calculated in the specified timezone without converting to UTC first. This prevents the day from changing which can change the month.
diffInSeconds()  : int
Get the difference in seconds
diffInWeekdays()  : int
Get the difference in weekdays
diffInWeekendDays()  : int
Get the difference in weekend days using a filter
diffInWeeks()  : int
Get the difference in weeks
diffInYears()  : int
Get the difference in years
disableLenientParsing()  : void
Enables lenient parsing for locale formats.
enableLenientParsing()  : void
Enables lenient parsing for locale formats.
endOfCentury()  : static
Sets the date to end of the century and time to 23:59:59
endOfDay()  : static
Sets the time to 23:59:59 or 23:59:59.999999 if `$microseconds` is true.
endOfDecade()  : static
Sets the date to end of the decade and time to 23:59:59
endOfMonth()  : static
Sets the date to end of the month and time to 23:59:59
endOfWeek()  : static
Sets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
endOfYear()  : static
Sets the date to end of the year and time to 23:59:59
eq()  : bool
Determines if the instance is equal to another
equals()  : bool
Determines if the instance is equal to another
farthest()  : ChronosInterface
Get the farthest date from the instance.
firstOfMonth()  : static
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.
firstOfQuarter()  : static
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.
firstOfYear()  : static
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.
fromNow()  : DateInterval|bool
Convenience method for getting the remaining time from a given time.
getDefaultLocale()  : string|null
Gets the default locale.
getDiffFormatter()  : DifferenceFormatterInterface
Get the difference formatter instance.
getLastErrors()  : array<string|int, mixed>
Returns any errors or warnings that were found during the parsing of the last object created by this class.
getTestNow()  : ChronosInterface|null
Get the ChronosInterface instance (real or mock) to be returned when a "now" instance is created.
getWeekendDays()  : array<string|int, mixed>
Get weekend days
getWeekEndsAt()  : int
Get the last day of week
getWeekStartsAt()  : int
Get the first day of week
greaterThan()  : bool
Determines if the instance is greater (after) than another
greaterThanOrEquals()  : bool
Determines if the instance is greater (after) than or equal to another
gt()  : bool
Determines if the instance is greater (after) than another
gte()  : bool
Determines if the instance is greater (after) than or equal to another
hasRelativeKeywords()  : bool
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
hasTestNow()  : bool
Determine if there is a valid test instance set. A valid test instance is anything that is not null.
hour()  : static
Set the instance's hour
i18nFormat()  : string|int
Returns a formatted string for this time object using the preferred format and language for the specified locale.
instance()  : static
Create a ChronosInterface instance from a DateTimeInterface one
isBirthday()  : bool
Check if its the birthday. Compares the date/month values of the two dates.
isFriday()  : bool
Checks if this day is a Friday.
isFuture()  : bool
Determines if the instance is in the future, ie. greater (after) than now
isLastMonth()  : bool
Determines if the instance is within the last month
isLastWeek()  : bool
Determines if the instance is within the last week
isLastYear()  : bool
Determines if the instance is within the last year
isLeapYear()  : bool
Determines if the instance is a leap year
isMonday()  : bool
Checks if this day is a Monday.
isMutable()  : bool
Check if instance of ChronosInterface is mutable.
isNextMonth()  : bool
Determines if the instance is within the next month
isNextWeek()  : bool
Determines if the instance is within the next week
isNextYear()  : bool
Determines if the instance is within the next year
isPast()  : bool
Determines if the instance is in the past, ie. less (before) than now
isSameDay()  : bool
Checks if the passed in date is the same day as the instance current day.
isSaturday()  : bool
Checks if this day is a Saturday.
isSunday()  : bool
Checks if this day is a Sunday.
isThisMonth()  : bool
Returns true if this object represents a date within the current month
isThisWeek()  : bool
Returns true if this object represents a date within the current week
isThisYear()  : bool
Returns true if this object represents a date within the current year
isThursday()  : bool
Checks if this day is a Thursday.
isToday()  : bool
Determines if the instance is today
isTomorrow()  : bool
Determines if the instance is tomorrow
isTuesday()  : bool
Checks if this day is a Tuesday.
isWednesday()  : bool
Checks if this day is a Wednesday.
isWeekday()  : bool
Determines if the instance is a weekday
isWeekend()  : bool
Determines if the instance is a weekend day
isWithinNext()  : bool
Returns true this instance will happen within the specified interval
isYesterday()  : bool
Determines if the instance is yesterday
jsonSerialize()  : string|int
Returns a string that should be serialized when converting this object to JSON
lastOfMonth()  : static
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.
lastOfQuarter()  : static
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.
lastOfYear()  : static
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.
lenientParsingEnabled()  : bool
Gets whether locale format parsing is set to lenient.
lessThan()  : bool
Determines if the instance is less (before) than another
lessThanOrEquals()  : bool
Determines if the instance is less (before) or equal to another
listTimezones()  : array<string|int, mixed>
Get list of timezone identifiers
lt()  : bool
Determines if the instance is less (before) than another
lte()  : bool
Determines if the instance is less (before) or equal to another
max()  : ChronosInterface
Get the maximum instance between a given instance (default now) and the current instance.
maxValue()  : ChronosInterface
Create a ChronosInterface instance for the greatest supported date.
microsecond()  : static
Set the instance's microsecond
min()  : ChronosInterface
Get the minimum instance between a given instance (default now) and the current instance.
minute()  : static
Set the instance's minute
minValue()  : ChronosInterface
Create a ChronosInterface instance for the lowest supported date.
month()  : static
Set the instance's month
ne()  : bool
Determines if the instance is not equal to another
next()  : static
Modify to the next occurrence of a given day of the week.
nice()  : string
Returns a nicely formatted date string for this object.
notEquals()  : bool
Determines if the instance is not equal to another
now()  : static
Get a ChronosInterface instance for the current date and time
nthOfMonth()  : static|false
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.
nthOfQuarter()  : static|false
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.
nthOfYear()  : static|false
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.
parse()  : 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()
parseDate()  : 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.
parseDateTime()  : 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.
parseTime()  : 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.
previous()  : static
Modify to the previous occurrence of a given day of the week.
resetToStringFormat()  : void
Resets the format used to the default when converting an instance of this type to a string
second()  : static
Set the instance's second
secondsSinceMidnight()  : int
The number of seconds since midnight.
secondsUntilEndOfDay()  : int
The number of seconds until 23:59:59.
setDate()  : static
Set the date to a different date.
setDateTime()  : static
Set the date and time all together
setDefaultLocale()  : void
Sets the default locale.
setDiffFormatter()  : void
Set the difference formatter instance.
setJsonEncodeFormat()  : void
setTestNow()  : void
Set a ChronosInterface instance (real or mock) to be returned when a "now" instance is created. The provided instance will be returned specifically under the following conditions: - A call to the static now() method, ex. ChronosInterface::now() - When a null (or blank string) is passed to the constructor or parse(), ex. new Chronos(null) - When the string "now" is passed to the constructor or parse(), ex. new Chronos('now') - When a string containing the desired time is passed to ChronosInterface::parse()
setTimeFromTimeString()  : static
Set the time by time string
setTimezone()  : static
Set the instance's timezone from a string or object
setToStringFormat()  : void
Sets the default format used when type converting instances of this type to string
setWeekendDays()  : void
Set weekend days
setWeekEndsAt()  : void
Set the last day of week
setWeekStartsAt()  : void
Set the first day of week
startOfCentury()  : static
Sets the date to the first day of the century and the time to 00:00:00
startOfDay()  : static
Sets the time to 00:00:00
startOfDecade()  : static
Sets the date to the first day of the decade and the time to 00:00:00
startOfMonth()  : static
Sets the date to the first day of the month and the time to 00:00:00
startOfWeek()  : static
Sets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
startOfYear()  : static
Sets the date to the first day of the year and the time to 00:00:00
subDay()  : static
Remove a day from the instance
subDays()  : static
Remove days from the instance
subHour()  : static
Remove an hour from the instance
subHours()  : static
Remove hours from the instance
subMinute()  : static
Remove a minute from the instance
subMinutes()  : static
Remove minutes from the instance
subMonth()  : static
Remove a month from the instance
subMonths()  : static
Remove months from the instance
subMonthsWithOverflow()  : static
Remove months with overflow from the instance.
subMonthWithOverflow()  : static
Remove a month with overflow from the instance.
subSecond()  : static
Remove a second from the instance
subSeconds()  : static
Remove seconds from the instance
subWeek()  : static
Remove a week from the instance
subWeekday()  : static
Remove a weekday from the instance
subWeekdays()  : static
Remove weekdays from the instance
subWeeks()  : static
Remove weeks to the instance
subYear()  : static
Remove a year from the instance
subYears()  : static
Remove years from the instance.
subYearsWithOverflow()  : static
Remove years with overflow from the instance
subYearWithOverflow()  : static
Remove a year with overflow from the instance.
timeAgoInWords()  : string
Returns either a relative or a formatted absolute date depending on the difference between the current time and this object.
timestamp()  : static
Set the instance's timestamp
timezone()  : static
Alias for setTimezone()
toAtomString()  : string
Format the instance as ATOM
toCookieString()  : string
Format the instance as COOKIE
toDateString()  : string
Format the instance as date
toDateTimeString()  : string
Format the instance as date and time
today()  : static
Create a ChronosInterface instance for today
toDayDateTimeString()  : string
Format the instance with day, date and time
toFormattedDateString()  : string
Format the instance as a readable date
toIso8601String()  : string
Format the instance as ISO8601
tomorrow()  : static
Create a ChronosInterface instance for tomorrow
toMutable()  : MutableDateTime
Create a new mutable instance from current immutable instance.
toQuarter()  : int|array<string|int, mixed>
Returns the quarter
toRfc1036String()  : string
Format the instance as RFC1036
toRfc1123String()  : string
Format the instance as RFC1123
toRfc2822String()  : string
Format the instance as RFC2822
toRfc3339String()  : string
Format the instance as RFC3339
toRfc822String()  : string
Format the instance as RFC822
toRfc850String()  : string
Format the instance as RFC850
toRssString()  : string
Format the instance as RSS
toTimeString()  : string
Format the instance as time
toUnixString()  : string
Returns a UNIX timestamp.
toW3cString()  : string
Format the instance as W3C
toWeek()  : int
tz()  : static
Alias for setTimezone()
wasWithinLast()  : bool
Returns true this instance happened within the specified interval
year()  : static
Set the instance's year
yesterday()  : static
Create a ChronosInterface instance for yesterday
_formatObject()  : string
Returns a translated and localized date string.
safeCreateDateTimeZone()  : DateTimeZone
Creates a DateTimeZone from a string or a DateTimeZone
isRelativeOnly()  : bool
Determines if there is no fixed date in the time string.
isTimeExpression()  : bool
Determine if there is just a time in the time string
setDateParent()  : static
Just calling to parent setDate It used in overwritten setDate

Constants

UNIX_TIMESTAMP_FORMAT

serialise the value as a Unix Timestamp

public string UNIX_TIMESTAMP_FORMAT = 'unixTimestampFormat'

Properties

$age read-only

public int $age

does a diffInYears() with default parameters

$dayOfWeek read-only

public int $dayOfWeek

1 (for Monday) through 7 (for Sunday)

$dayOfYear read-only

public int $dayOfYear

0 through 365

$daysInMonth read-only

public int $daysInMonth

number of days in the given month

$dst read-only

public bool $dst

daylight savings time indicator, true if DST, false otherwise

$local read-only

public bool $local

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

$microsecond read-only

public int $microsecond

$niceFormat

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

public static array<string|int, int>|string|int $niceFormat = [\IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT]

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 (https://unicode-org.github.io/icu-docs/apidoc/released/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.

Tags
see
FrozenTime::nice()

$offset read-only

public int $offset

the timezone offset in seconds from UTC

$offsetHours read-only

public int $offsetHours

the timezone offset in hours from UTC

$quarter read-only

public int $quarter

the quarter of this instance, 1 - 4

$timestamp read-only

public int $timestamp

seconds since the Unix Epoch

$timezone read-only

public DateTimeZone $timezone

the current timezone

$timezoneName read-only

public string $timezoneName

$tz read-only

public DateTimeZone $tz

alias of timezone

$tzName read-only

public string $tzName

$utc read-only

public bool $utc

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

$weekOfMonth read-only

public int $weekOfMonth

1 through 5

$weekOfYear read-only

public int $weekOfYear

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

$wordAccuracy

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

public static array<string|int, string> $wordAccuracy = ['year' => 'day', 'month' => 'day', 'week' => 'day', 'day' => 'hour', 'hour' => 'minute', 'minute' => 'minute', 'second' => 'second']
Tags
see
FrozenTime::timeAgoInWords()

$wordFormat

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

public static array<string|int, int>|string|int $wordFormat = [\IntlDateFormatter::SHORT, \IntlDateFormatter::NONE]
Tags
see
FrozenTime::timeAgoInWords()

$yearIso read-only

public int $yearIso

$_formatters

In-memory cache of date formatters

protected static array<string|int, IntlDateFormatter> $_formatters = []

$_jsonEncodeFormat

The format to use when converting this object to JSON.

protected static Closure|array<string|int, int>|string|int $_jsonEncodeFormat = "yyyy-MM-dd'T'HH':'mm':'ssxxx"

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 (https://unicode-org.github.io/icu-docs/apidoc/released/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.

Tags
see
Time::i18nFormat()

$_lastErrors

Holds the last error generated by createFromFormat

protected static array<string|int, mixed> $_lastErrors = []

$_toStringFormat

The format to use when formatting a time using `Cake\I18n\FrozenTime::i18nFormat()` and `__toString`. This format is also used by `parseDateTime()`.

protected static array<string|int, int>|string|int $_toStringFormat = [\IntlDateFormatter::SHORT, \IntlDateFormatter::SHORT]

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 (https://unicode-org.github.io/icu-docs/apidoc/released/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.

Tags
see
FrozenTime::i18nFormat()

$days

Names of days of the week.

protected static array<string|int, mixed> $days = [\Cake\Chronos\ChronosInterface::MONDAY => 'Monday', \Cake\Chronos\ChronosInterface::TUESDAY => 'Tuesday', \Cake\Chronos\ChronosInterface::WEDNESDAY => 'Wednesday', \Cake\Chronos\ChronosInterface::THURSDAY => 'Thursday', \Cake\Chronos\ChronosInterface::FRIDAY => 'Friday', \Cake\Chronos\ChronosInterface::SATURDAY => 'Saturday', \Cake\Chronos\ChronosInterface::SUNDAY => 'Sunday']

$defaultLocale

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

protected static string|null $defaultLocale

Use static::setDefaultLocale() and static::getDefaultLocale() instead.

$lenientParsing

Whether lenient parsing is enabled for IntlDateFormatter.

protected static bool $lenientParsing = true

Defaults to true which is the default for IntlDateFormatter.

$relativePattern

Regex for relative period.

protected static string $relativePattern = '/this|next|last|tomorrow|yesterday|midnight|today|[+-]|first|last|ago/i'

$testNow

A test ChronosInterface instance to be returned when now instances are created

protected static ChronosInterface|null $testNow

There is a single test now for all date/time classes provided by Chronos. This aims to emulate stubbing out 'now' which is a single global fact.

$toStringFormat

Format to use for __toString method when type juggling occurs.

protected static string $toStringFormat = \Cake\Chronos\ChronosInterface::DEFAULT_TO_STRING_FORMAT

$weekendDays

Days of weekend

protected static array<string|int, mixed> $weekendDays = [\Cake\Chronos\ChronosInterface::SATURDAY, \Cake\Chronos\ChronosInterface::SUNDAY]

$weekEndsAt

Last day of week

protected static int $weekEndsAt = \Cake\Chronos\ChronosInterface::SUNDAY

$weekStartsAt

First day of week

protected static int $weekStartsAt = \Cake\Chronos\ChronosInterface::MONDAY

Methods

__construct()

Create a new immutable time instance.

public __construct([DateTimeInterface|string|int|null $time = null ][, DateTimeZone|string|null $tz = null ]) : mixed
Parameters
$time : DateTimeInterface|string|int|null = null

Fixed or relative time

$tz : DateTimeZone|string|null = null

The timezone for the instance

__debugInfo()

Returns the data that should be displayed when debugging this object

public __debugInfo() : array<string, mixed>
Return values
array<string, mixed>

__get()

Get a part of the ChronosInterface object

public __get(string $name) : string|int|bool|DateTimeZone
Parameters
$name : string

The property name to read.

Tags
throws
InvalidArgumentException
Return values
string|int|bool|DateTimeZone

The property value.

__isset()

Check if an attribute exists on the object

public __isset(string $name) : bool
Parameters
$name : string

The property name to check.

Return values
bool

Whether or not the property exists.

__toString()

public __toString() : string
Tags
inheritDoc
Return values
string

addDay()

Add a day to the instance

public addDay([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of days to add.

Return values
static

addDays()

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

public addDays(int $value) : static
Parameters
$value : int

The number of days to add.

Return values
static

addHour()

Add an hour to the instance

public addHour([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of hours to add.

Return values
static

addHours()

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

public addHours(int $value) : static
Parameters
$value : int

The number of hours to add.

Return values
static

addMinute()

Add a minute to the instance

public addMinute([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of minutes to add.

Return values
static

addMinutes()

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

public addMinutes(int $value) : static
Parameters
$value : int

The number of minutes to add.

Return values
static

addMonth()

Add a month to the instance

public addMonth([int $value = 1 ]) : static

Has the same behavior as addMonths().

Parameters
$value : int = 1

The number of months to add.

Return values
static

addMonths()

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

public addMonths(int $value) : static

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
$value : int

The number of months to add.

Return values
static

addMonthsWithOverflow()

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

public addMonthsWithOverflow(int $value) : static

If the new date does not exist, the days overflow into the next month.

Example:

 (new Chronos('2012-01-30'))->addMonthsWithOverflow(1); // Results in 2013-03-01
Parameters
$value : int

The number of months to add.

Return values
static

addMonthWithOverflow()

Add a month with overflow to the instance.

public addMonthWithOverflow([int $value = 1 ]) : static

Has the same behavior as addMonthsWithOverflow().

Parameters
$value : int = 1

The number of months to add.

Return values
static

addSecond()

Add a second to the instance

public addSecond([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of seconds to add.

Return values
static

addSeconds()

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

public addSeconds(int $value) : static
Parameters
$value : int

The number of seconds to add.

Return values
static

addWeek()

Add a week to the instance

public addWeek([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of weeks to add.

Return values
static

addWeekday()

Add a weekday to the instance

public addWeekday([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of weekdays to add.

Return values
static

addWeekdays()

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

public addWeekdays(int $value) : static
Parameters
$value : int

The number of weekdays to add.

Return values
static

addWeeks()

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

public addWeeks(int $value) : static
Parameters
$value : int

The number of weeks to add.

Return values
static

addYear()

Add a year to the instance.

public addYear([int $value = 1 ]) : static

Has the same behavior as addYears().

Parameters
$value : int = 1

The number of years to add.

Return values
static

addYears()

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

public addYears(int $value) : static

If the new date does not exist, the last day of the month is used instead instead of overflowing into the next month.

Example:

 (new Chronos('2015-01-03'))->addYears(1); // Results in 2016-01-03

 (new Chronos('2012-02-29'))->addYears(1); // Results in 2013-02-28
Parameters
$value : int

The number of years to add.

Return values
static

addYearsWithOverflow()

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

public addYearsWithOverflow(int $value) : static

If the new date does not exist, the days overflow into the next month.

Example:

 (new Chronos('2012-02-29'))->addYearsWithOverflow(1); // Results in 2013-03-01
Parameters
$value : int

The number of years to add.

Return values
static

addYearWithOverflow()

Add a year with overflow to the instance.

public addYearWithOverflow([int $value = 1 ]) : static

Has the same behavior as addYearsWithOverflow().

Parameters
$value : int = 1

The number of years to add.

Return values
static

average()

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

public average([ChronosInterface|null $dt = null ]) : static
Parameters
$dt : ChronosInterface|null = null

The instance to compare with.

Return values
static

copy()

Get a copy of the instance

public copy() : static
Return values
static

create()

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

public static create([int|null $year = null ][, int|null $month = null ][, int|null $day = null ][, int|null $hour = null ][, int|null $minute = null ][, int|null $second = null ][, int|null $microsecond = null ][, DateTimeZone|string|null $tz = null ]) : static

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, $second and $microsecond will be their now() values. If $hour is not null then the default values for $minute, $second and $microsecond will be 0.

Parameters
$year : int|null = null

The year to create an instance with.

$month : int|null = null

The month to create an instance with.

$day : int|null = null

The day to create an instance with.

$hour : int|null = null

The hour to create an instance with.

$minute : int|null = null

The minute to create an instance with.

$second : int|null = null

The second to create an instance with.

$microsecond : int|null = null

The microsecond to create an instance with.

$tz : DateTimeZone|string|null = null

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

Return values
static

createFromArray()

Creates a ChronosInterface instance from an array of date and time values.

public static createFromArray(array<string|int, int|string> $values) : static

The 'year', 'month' and 'day' values must all be set for a date. The time values all default to 0.

The 'timezone' value can be any format supported by \DateTimeZone.

Allowed values:

  • year
  • month
  • day
  • hour
  • minute
  • second
  • microsecond
  • meridian ('am' or 'pm')
  • timezone
Parameters
$values : array<string|int, int|string>

Array of date and time values.

Return values
static

createFromDate()

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

public static createFromDate([int|null $year = null ][, int|null $month = null ][, int|null $day = null ][, DateTimeZone|string|null $tz = null ]) : static
Parameters
$year : int|null = null

The year to create an instance with.

$month : int|null = null

The month to create an instance with.

$day : int|null = null

The day to create an instance with.

$tz : DateTimeZone|string|null = null

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

Return values
static

createFromFormat()

Create a ChronosInterface instance from a specific format

public static createFromFormat(string $format, string $time[, DateTimeZone|string|null $tz = null ]) : static
Parameters
$format : string

The date() compatible format string.

$time : string

The formatted date string to interpret.

$tz : DateTimeZone|string|null = null

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

Tags
throws
InvalidArgumentException
Attributes
#[ReturnTypeWillChange]
Return values
static

createFromTime()

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

public static createFromTime([int|null $hour = null ][, int|null $minute = null ][, int|null $second = null ][, int|null $microsecond = null ][, DateTimeZone|string|null $tz = null ]) : static
Parameters
$hour : int|null = null

The hour to create an instance with.

$minute : int|null = null

The minute to create an instance with.

$second : int|null = null

The second to create an instance with.

$microsecond : int|null = null

The microsecond to create an instance with.

$tz : DateTimeZone|string|null = null

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

Return values
static

createFromTimestamp()

Create a ChronosInterface instance from a timestamp

public static createFromTimestamp(int $timestamp[, DateTimeZone|string|null $tz = null ]) : static
Parameters
$timestamp : int

The timestamp to create an instance from.

$tz : DateTimeZone|string|null = null

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

Return values
static

createFromTimestampUTC()

Create a ChronosInterface instance from an UTC timestamp

public static createFromTimestampUTC(int $timestamp) : static
Parameters
$timestamp : int

The UTC timestamp to create an instance from.

Return values
static

createInterval()

Create a new DateInterval instance from specified values.

public static createInterval([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 ]) : DateInterval
Parameters
$years : int|null = null

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
DateInterval

day()

Set the instance's day

public day(int $value) : static
Parameters
$value : int

The day value.

Return values
static

diffFiltered()

Get the difference by the given interval using a filter callable

public diffFiltered(ChronosInterval|DateInterval $ci, callable $callback[, ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$ci : ChronosInterval|DateInterval

An interval to traverse by

$callback : callable

The callback to use for filtering.

$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffForHumans()

Get the difference in a human readable format.

public diffForHumans([ChronosInterface|null $other = null ][, bool $absolute = false ]) : string

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
$other : ChronosInterface|null = null

The datetime to compare with.

$absolute : bool = false

removes time difference modifiers ago, after, etc

Return values
string

diffInDays()

Get the difference in days

public diffInDays([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInDaysFiltered()

Get the difference in days using a filter callable

public diffInDaysFiltered(callable $callback[, ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$callback : callable

The callback to use for filtering.

$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInHours()

Get the difference in hours

public diffInHours([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInHoursFiltered()

Get the difference in hours using a filter callable

public diffInHoursFiltered(callable $callback[, ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$callback : callable

The callback to use for filtering.

$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInMinutes()

Get the difference in minutes

public diffInMinutes([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInMonths()

Get the difference in months

public diffInMonths([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInMonthsIgnoreTimezone()

Get the difference in months ignoring the timezone. This means the months are calculated in the specified timezone without converting to UTC first. This prevents the day from changing which can change the month.

public diffInMonthsIgnoreTimezone([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int

For example, if comparing 2019-06-01 Asia/Tokyo and 2019-10-01 Asia/Tokyo, the result would be 4 months instead of 3 when using normal DateTime::diff().

Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInSeconds()

Get the difference in seconds

public diffInSeconds([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInWeekdays()

Get the difference in weekdays

public diffInWeekdays([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInWeekendDays()

Get the difference in weekend days using a filter

public diffInWeekendDays([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInWeeks()

Get the difference in weeks

public diffInWeeks([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

diffInYears()

Get the difference in years

public diffInYears([ChronosInterface|null $dt = null ][, bool $abs = true ]) : int
Parameters
$dt : ChronosInterface|null = null

The instance to difference from.

$abs : bool = true

Get the absolute of the difference

Return values
int

disableLenientParsing()

Enables lenient parsing for locale formats.

public static disableLenientParsing() : void

enableLenientParsing()

Enables lenient parsing for locale formats.

public static enableLenientParsing() : void

endOfCentury()

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

public endOfCentury() : static
Return values
static

endOfDay()

Sets the time to 23:59:59 or 23:59:59.999999 if `$microseconds` is true.

public endOfDay([bool $microseconds = false ]) : static
Parameters
$microseconds : bool = false

Whether to set microseconds

Return values
static

endOfDecade()

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

public endOfDecade() : static
Return values
static

endOfMonth()

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

public endOfMonth() : static
Return values
static

endOfWeek()

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

public endOfWeek() : static
Return values
static

endOfYear()

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

public endOfYear() : static
Return values
static

firstOfMonth()

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.

public firstOfMonth([int|null $dayOfWeek = null ]) : static
Parameters
$dayOfWeek : int|null = null

The day of the week to move to.

Return values
static

firstOfQuarter()

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.

public firstOfQuarter([int|null $dayOfWeek = null ]) : static
Parameters
$dayOfWeek : int|null = null

The day of the week to move to.

Return values
static

firstOfYear()

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.

public firstOfYear([int|null $dayOfWeek = null ]) : static
Parameters
$dayOfWeek : int|null = null

The day of the week to move to.

Return values
static

fromNow()

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

public static fromNow(DateTime|DateTimeImmutable $datetime) : DateInterval|bool
Parameters
$datetime : DateTime|DateTimeImmutable

The date to get the remaining time from.

Return values
DateInterval|bool

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

getDefaultLocale()

Gets the default locale.

public static getDefaultLocale() : string|null
Return values
string|null

The default locale string to be used or null.

getLastErrors()

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

public static getLastErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>

getTestNow()

Get the ChronosInterface instance (real or mock) to be returned when a "now" instance is created.

public static getTestNow() : ChronosInterface|null
Return values
ChronosInterface|null

The current instance used for testing

getWeekendDays()

Get weekend days

public static getWeekendDays() : array<string|int, mixed>
Return values
array<string|int, mixed>

getWeekEndsAt()

Get the last day of week

public static getWeekEndsAt() : int
Return values
int

getWeekStartsAt()

Get the first day of week

public static getWeekStartsAt() : int
Return values
int

greaterThanOrEquals()

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

public greaterThanOrEquals(ChronosInterface $dt) : bool
Parameters
$dt : ChronosInterface

The instance to compare with.

Return values
bool

gt()

Determines if the instance is greater (after) than another

public gt(ChronosInterface $dt) : bool

gt() is deprecated. Use greaterThan() instead.

Parameters
$dt : ChronosInterface

The instance to compare with.

Return values
bool

gte()

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

public gte(ChronosInterface $dt) : bool

gte() is deprecated. Use greaterThanOrEquals() instead.

Parameters
$dt : ChronosInterface

The instance to compare with.

Return values
bool

hasRelativeKeywords()

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

public static hasRelativeKeywords(string|null $time) : bool
Parameters
$time : string|null

The time string to check.

Return values
bool

true if there is a keyword, otherwise false

hasTestNow()

Determine if there is a valid test instance set. A valid test instance is anything that is not null.

public static hasTestNow() : bool
Return values
bool

True if there is a test instance, otherwise false

hour()

Set the instance's hour

public hour(int $value) : static
Parameters
$value : int

The hour value.

Return values
static

i18nFormat()

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

public i18nFormat([array<string|int, int>|string|int|null $format = null ][, DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : string|int

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: https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax.

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'

You can control the default format used through Time::setToStringFormat().

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 used through Time::setDefaultLocale(). If empty, the default will be taken from the intl.default_locale ini config.

Parameters
$format : array<string|int, int>|string|int|null = null

Format string.

$timezone : DateTimeZone|string|null = null

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

$locale : string|null = null

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

Return values
string|int

Formatted and translated date string

instance()

Create a ChronosInterface instance from a DateTimeInterface one

public static instance(DateTimeInterface $dt) : static
Parameters
$dt : DateTimeInterface

The datetime instance to convert.

Return values
static

isBirthday()

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

public isBirthday([ChronosInterface|null $dt = null ]) : bool
Parameters
$dt : ChronosInterface|null = null

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

Return values
bool

isFriday()

Checks if this day is a Friday.

public isFriday() : bool
Return values
bool

isFuture()

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

public isFuture() : bool
Return values
bool

isLastMonth()

Determines if the instance is within the last month

public isLastMonth() : bool
Return values
bool

isLastWeek()

Determines if the instance is within the last week

public isLastWeek() : bool
Return values
bool

isLastYear()

Determines if the instance is within the last year

public isLastYear() : bool
Return values
bool

isLeapYear()

Determines if the instance is a leap year

public isLeapYear() : bool
Return values
bool

isMonday()

Checks if this day is a Monday.

public isMonday() : bool
Return values
bool

isMutable()

Check if instance of ChronosInterface is mutable.

public isMutable() : bool
Return values
bool

isNextMonth()

Determines if the instance is within the next month

public isNextMonth() : bool
Return values
bool

isNextWeek()

Determines if the instance is within the next week

public isNextWeek() : bool
Return values
bool

isNextYear()

Determines if the instance is within the next year

public isNextYear() : bool
Return values
bool

isPast()

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

public isPast() : bool
Return values
bool

isSaturday()

Checks if this day is a Saturday.

public isSaturday() : bool
Return values
bool

isSunday()

Checks if this day is a Sunday.

public isSunday() : bool
Return values
bool

isThisMonth()

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

public isThisMonth() : bool
Return values
bool

isThisWeek()

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

public isThisWeek() : bool
Return values
bool

isThisYear()

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

public isThisYear() : bool
Return values
bool

isThursday()

Checks if this day is a Thursday.

public isThursday() : bool
Return values
bool

isToday()

Determines if the instance is today

public isToday() : bool
Return values
bool

isTomorrow()

Determines if the instance is tomorrow

public isTomorrow() : bool
Return values
bool

isTuesday()

Checks if this day is a Tuesday.

public isTuesday() : bool
Return values
bool

isWednesday()

Checks if this day is a Wednesday.

public isWednesday() : bool
Return values
bool

isWeekday()

Determines if the instance is a weekday

public isWeekday() : bool
Return values
bool

isWeekend()

Determines if the instance is a weekend day

public isWeekend() : bool
Return values
bool

isWithinNext()

Returns true this instance will happen within the specified interval

public isWithinNext(string|int $timeInterval) : bool
Parameters
$timeInterval : string|int

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

Return values
bool

isYesterday()

Determines if the instance is yesterday

public isYesterday() : bool
Return values
bool

jsonSerialize()

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

public jsonSerialize() : string|int
Attributes
#[ReturnTypeWillChange]
Return values
string|int

lastOfMonth()

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.

public lastOfMonth([int|null $dayOfWeek = null ]) : static
Parameters
$dayOfWeek : int|null = null

The day of the week to move to.

Return values
static

lastOfQuarter()

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.

public lastOfQuarter([int|null $dayOfWeek = null ]) : static
Parameters
$dayOfWeek : int|null = null

The day of the week to move to.

Return values
static

lastOfYear()

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.

public lastOfYear([int|null $dayOfWeek = null ]) : static
Parameters
$dayOfWeek : int|null = null

The day of the week to move to.

Return values
static

lenientParsingEnabled()

Gets whether locale format parsing is set to lenient.

public static lenientParsingEnabled() : bool
Return values
bool

listTimezones()

Get list of timezone identifiers

public static listTimezones([string|int|null $filter = null ][, string|null $country = null ][, array<string, mixed>|bool $options = [] ]) : array<string|int, mixed>
Parameters
$filter : string|int|null = null

A regex to filter identifier Or one of DateTimeZone class constants

$country : string|null = null

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

$options : array<string, mixed>|bool = []

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.

Tags
since
2.2
Return values
array<string|int, mixed>

List of timezone identifiers

lte()

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

public lte(ChronosInterface $dt) : bool

lte() is deprecated. Use lessThanOrEquals() instead.

Parameters
$dt : ChronosInterface

The instance to compare with.

Return values
bool

microsecond()

Set the instance's microsecond

public microsecond(int $value) : static
Parameters
$value : int

The microsecond value.

Return values
static

minute()

Set the instance's minute

public minute(int $value) : static
Parameters
$value : int

The minute value.

Return values
static

month()

Set the instance's month

public month(int $value) : static
Parameters
$value : int

The month value.

Return values
static

next()

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

public next([int|null $dayOfWeek = null ]) : static

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
$dayOfWeek : int|null = null

The day of the week to move to.

Return values
static

nice()

Returns a nicely formatted date string for this object.

public nice([DateTimeZone|string|null $timezone = null ][, string|null $locale = null ]) : string

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

Parameters
$timezone : DateTimeZone|string|null = null

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

$locale : string|null = null

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

Return values
string

Formatted date string

now()

Get a ChronosInterface instance for the current date and time

public static now([DateTimeZone|string|null $tz = null ]) : static
Parameters
$tz : DateTimeZone|string|null = null

The DateTimeZone object or timezone name.

Return values
static

nthOfMonth()

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.

public nthOfMonth(int $nth, int $dayOfWeek) : static|false

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

Parameters
$nth : int

The offset to use.

$dayOfWeek : int

The day of the week to move to.

Return values
static|false

nthOfQuarter()

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.

public nthOfQuarter(int $nth, int $dayOfWeek) : static|false

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

Parameters
$nth : int

The offset to use.

$dayOfWeek : int

The day of the week to move to.

Return values
static|false

nthOfYear()

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.

public nthOfYear(int $nth, int $dayOfWeek) : static|false

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

Parameters
$nth : int

The offset to use.

$dayOfWeek : int

The day of the week to move to.

Return values
static|false

parse()

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

public static parse([DateTimeInterface|string|int $time = 'now' ][, DateTimeZone|string|null $tz = null ]) : static
Parameters
$time : DateTimeInterface|string|int = 'now'

The strtotime compatible string to parse

$tz : DateTimeZone|string|null = null

The DateTimeZone object or timezone name.

Return values
static

parseDate()

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.

public static parseDate(string $date[, array<string|int, mixed>|string|int|null $format = null ]) : static|null

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
$date : string

The date string to parse.

$format : array<string|int, mixed>|string|int|null = null

Any format accepted by IntlDateFormatter.

Return values
static|null

parseDateTime()

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.

public static parseDateTime(string $time[, array<string|int, int>|string|int|null $format = null ][, DateTimeZone|string|null $tz = null ]) : static|null

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

Unlike DateTime, the time zone of the returned instance is always converted to $tz (default time zone if null) even if the $time string specified a time zone. This is a limitation of IntlDateFormatter.

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, IntlDateFormatter::NONE]);
Parameters
$time : string

The time string to parse.

$format : array<string|int, int>|string|int|null = null

Any format accepted by IntlDateFormatter.

$tz : DateTimeZone|string|null = null

The timezone for the instance

Return values
static|null

parseTime()

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.

public static parseTime(string $time[, string|int|null $format = null ]) : static|null

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
$time : string

The time string to parse.

$format : string|int|null = null

Any format accepted by IntlDateFormatter.

Return values
static|null

previous()

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

public previous([int|null $dayOfWeek = null ]) : static

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
$dayOfWeek : int|null = null

The day of the week to move to.

Return values
static

resetToStringFormat()

Resets the format used to the default when converting an instance of this type to a string

public static resetToStringFormat() : void

second()

Set the instance's second

public second(int $value) : static
Parameters
$value : int

The seconds value.

Return values
static

secondsSinceMidnight()

The number of seconds since midnight.

public secondsSinceMidnight() : int
Return values
int

secondsUntilEndOfDay()

The number of seconds until 23:59:59.

public secondsUntilEndOfDay() : int
Return values
int

setDate()

Set the date to a different date.

public setDate(int $year, int $month, int $day) : static

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

Parameters
$year : int

The year to set.

$month : int

The month to set.

$day : int

The day to set.

Attributes
#[ReturnTypeWillChange]
Return values
static

setDateTime()

Set the date and time all together

public setDateTime(int $year, int $month, int $day, int $hour, int $minute[, int $second = 0 ]) : static
Parameters
$year : int

The year to set.

$month : int

The month to set.

$day : int

The day to set.

$hour : int

The hour to set.

$minute : int

The minute to set.

$second : int = 0

The second to set.

Return values
static

setDefaultLocale()

Sets the default locale.

public static setDefaultLocale([string|null $locale = null ]) : void

Set to null to use IntlDateFormatter default.

Parameters
$locale : string|null = null

The default locale string to be used.

setJsonEncodeFormat()

public static setJsonEncodeFormat(mixed $format) : void
Parameters
$format : mixed
Tags
inheritDoc

setTestNow()

Set a ChronosInterface instance (real or mock) to be returned when a "now" instance is created. The provided instance will be returned specifically under the following conditions: - A call to the static now() method, ex. ChronosInterface::now() - When a null (or blank string) is passed to the constructor or parse(), ex. new Chronos(null) - When the string "now" is passed to the constructor or parse(), ex. new Chronos('now') - When a string containing the desired time is passed to ChronosInterface::parse()

public static setTestNow([ChronosInterface|string|null $testNow = null ]) : void

Note the timezone parameter was left out of the examples above and has no affect as the mock value will be returned regardless of its value.

To clear the test instance call this method using the default parameter of null.

Parameters
$testNow : ChronosInterface|string|null = null

The instance to use for all future instances.

setTimeFromTimeString()

Set the time by time string

public setTimeFromTimeString(string $time) : static
Parameters
$time : string

Time as string.

Return values
static

setTimezone()

Set the instance's timezone from a string or object

public setTimezone(DateTimeZone|string $value) : static
Parameters
$value : DateTimeZone|string

The DateTimeZone object or timezone name to use.

Attributes
#[ReturnTypeWillChange]
Return values
static

setToStringFormat()

Sets the default format used when type converting instances of this type to string

public static setToStringFormat(array<string|int, int>|string|int $format) : void

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 (https://unicode-org.github.io/icu-docs/apidoc/released/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.

Parameters
$format : array<string|int, int>|string|int

Format.

setWeekendDays()

Set weekend days

public static setWeekendDays(array<string|int, mixed> $days) : void
Parameters
$days : array<string|int, mixed>

Which days are 'weekends'.

setWeekEndsAt()

Set the last day of week

public static setWeekEndsAt(int $day) : void
Parameters
$day : int

The day the week ends with.

setWeekStartsAt()

Set the first day of week

public static setWeekStartsAt(int $day) : void
Parameters
$day : int

The day the week starts with.

startOfCentury()

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

public startOfCentury() : static
Return values
static

startOfDay()

Sets the time to 00:00:00

public startOfDay() : static
Return values
static

startOfDecade()

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

public startOfDecade() : static
Return values
static

startOfMonth()

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

public startOfMonth() : static
Return values
static

startOfWeek()

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

public startOfWeek() : static
Return values
static

startOfYear()

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

public startOfYear() : static
Return values
static

subDay()

Remove a day from the instance

public subDay([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of days to remove.

Return values
static

subDays()

Remove days from the instance

public subDays(int $value) : static
Parameters
$value : int

The number of days to remove.

Return values
static

subHour()

Remove an hour from the instance

public subHour([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of hours to remove.

Return values
static

subHours()

Remove hours from the instance

public subHours(int $value) : static
Parameters
$value : int

The number of hours to remove.

Return values
static

subMinute()

Remove a minute from the instance

public subMinute([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of minutes to remove.

Return values
static

subMinutes()

Remove minutes from the instance

public subMinutes(int $value) : static
Parameters
$value : int

The number of minutes to remove.

Return values
static

subMonth()

Remove a month from the instance

public subMonth([int $value = 1 ]) : static

Has the same behavior as addMonths().

Parameters
$value : int = 1

The number of months to remove.

Return values
static

subMonths()

Remove months from the instance

public subMonths(int $value) : static

Has the same behavior as addMonths().

Parameters
$value : int

The number of months to remove.

Return values
static

subMonthsWithOverflow()

Remove months with overflow from the instance.

public subMonthsWithOverflow(int $value) : static

Has the same behavior as addMonthsWithOverflow().

Parameters
$value : int

The number of months to remove.

Return values
static

subMonthWithOverflow()

Remove a month with overflow from the instance.

public subMonthWithOverflow([int $value = 1 ]) : static

Has the same behavior as addMonthsWithOverflow().

Parameters
$value : int = 1

The number of months to remove.

Return values
static

subSecond()

Remove a second from the instance

public subSecond([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of seconds to remove.

Return values
static

subSeconds()

Remove seconds from the instance

public subSeconds(int $value) : static
Parameters
$value : int

The number of seconds to remove.

Return values
static

subWeek()

Remove a week from the instance

public subWeek([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of weeks to remove.

Return values
static

subWeekday()

Remove a weekday from the instance

public subWeekday([int $value = 1 ]) : static
Parameters
$value : int = 1

The number of weekdays to remove.

Return values
static

subWeekdays()

Remove weekdays from the instance

public subWeekdays(int $value) : static
Parameters
$value : int

The number of weekdays to remove.

Return values
static

subWeeks()

Remove weeks to the instance

public subWeeks(int $value) : static
Parameters
$value : int

The number of weeks to remove.

Return values
static

subYear()

Remove a year from the instance

public subYear([int $value = 1 ]) : static

Has the same behavior as addYears().

Parameters
$value : int = 1

The number of years to remove.

Return values
static

subYears()

Remove years from the instance.

public subYears(int $value) : static

Has the same behavior as addYears().

Parameters
$value : int

The number of years to remove.

Return values
static

subYearsWithOverflow()

Remove years with overflow from the instance

public subYearsWithOverflow(int $value) : static

Has the same behavior as addYeasrWithOverflow().

Parameters
$value : int

The number of years to remove.

Return values
static

subYearWithOverflow()

Remove a year with overflow from the instance.

public subYearWithOverflow([int $value = 1 ]) : static

Has the same behavior as addYearsWithOverflow().

Parameters
$value : int = 1

The number of years to remove.

Return values
static

timeAgoInWords()

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

public timeAgoInWords([array<string, mixed> $options = [] ]) : string

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
$options : array<string, mixed> = []

Array of options.

Return values
string

Relative time string.

timestamp()

Set the instance's timestamp

public timestamp(int $value) : static
Parameters
$value : int

The timestamp value to set.

Return values
static

timezone()

Alias for setTimezone()

public timezone(DateTimeZone|string $value) : static
Parameters
$value : DateTimeZone|string

The DateTimeZone object or timezone name to use.

Return values
static

toAtomString()

Format the instance as ATOM

public toAtomString() : string
Return values
string

toCookieString()

Format the instance as COOKIE

public toCookieString() : string
Return values
string

toDateString()

Format the instance as date

public toDateString() : string
Return values
string

toDateTimeString()

Format the instance as date and time

public toDateTimeString() : string
Return values
string

today()

Create a ChronosInterface instance for today

public static today([DateTimeZone|string|null $tz = null ]) : static
Parameters
$tz : DateTimeZone|string|null = null

The timezone to use.

Return values
static

toDayDateTimeString()

Format the instance with day, date and time

public toDayDateTimeString() : string
Return values
string

toFormattedDateString()

Format the instance as a readable date

public toFormattedDateString() : string
Return values
string

toIso8601String()

Format the instance as ISO8601

public toIso8601String() : string
Return values
string

tomorrow()

Create a ChronosInterface instance for tomorrow

public static tomorrow([DateTimeZone|string|null $tz = null ]) : static
Parameters
$tz : DateTimeZone|string|null = null

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

Return values
static

toQuarter()

Returns the quarter

public toQuarter([bool $range = false ]) : int|array<string|int, mixed>
Parameters
$range : bool = false

Range.

Return values
int|array<string|int, mixed>

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

toRssString()

Format the instance as RSS

public toRssString() : string
Return values
string

toTimeString()

Format the instance as time

public toTimeString() : string
Return values
string

toUnixString()

Returns a UNIX timestamp.

public toUnixString() : string
Return values
string

UNIX timestamp

toW3cString()

Format the instance as W3C

public toW3cString() : string
Return values
string

tz()

Alias for setTimezone()

public tz(DateTimeZone|string $value) : static
Parameters
$value : DateTimeZone|string

The DateTimeZone object or timezone name to use.

Return values
static

wasWithinLast()

Returns true this instance happened within the specified interval

public wasWithinLast(string|int $timeInterval) : bool
Parameters
$timeInterval : string|int

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

Return values
bool

year()

Set the instance's year

public year(int $value) : static
Parameters
$value : int

The year value.

Return values
static

yesterday()

Create a ChronosInterface instance for yesterday

public static yesterday([DateTimeZone|string|null $tz = null ]) : static
Parameters
$tz : DateTimeZone|string|null = null

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

Return values
static

_formatObject()

Returns a translated and localized date string.

protected _formatObject(DateTime|DateTimeImmutable $date, array<string|int, int>|string|int $format, string|null $locale) : string

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

Parameters
$date : DateTime|DateTimeImmutable

Date.

$format : array<string|int, int>|string|int

Format.

$locale : string|null

The locale name in which the date should be displayed.

Return values
string

safeCreateDateTimeZone()

Creates a DateTimeZone from a string or a DateTimeZone

protected static safeCreateDateTimeZone(DateTimeZone|string|null $object) : DateTimeZone
Parameters
$object : DateTimeZone|string|null

The value to convert.

Tags
throws
InvalidArgumentException
Return values
DateTimeZone

isRelativeOnly()

Determines if there is no fixed date in the time string.

private static isRelativeOnly(DateTimeInterface|string|null $time) : bool
Parameters
$time : DateTimeInterface|string|null

The time string to check

Return values
bool

true if doesn't contain a fixed date

isTimeExpression()

Determine if there is just a time in the time string

private static isTimeExpression(string $time) : bool
Parameters
$time : string

The time string to check.

Return values
bool

true if there is a keyword, otherwise false

setDateParent()

Just calling to parent setDate It used in overwritten setDate

private setDateParent(int $year, int $month, int $day) : static
Parameters
$year : int

The year to set.

$month : int

The month to set.

$day : int

The day to set.

Return values
static

        
On this page

Search results