Class Chronos
An Immutable extension on the native DateTime object.
Adds a number of convenience APIs methods and the ability to easily convert into a mutable object.
Constants
-
int
DAYS_PER_WEEK ¶7
-
string
DEFAULT_TO_STRING_FORMAT ¶'Y-m-d H:i:s'
Default format to use for __toString method when type juggling occurs.
-
int
FRIDAY ¶5
-
int
HOURS_PER_DAY ¶24
-
int
MINUTES_PER_HOUR ¶60
-
int
MONDAY ¶1
-
int
MONTHS_PER_QUARTER ¶3
-
int
MONTHS_PER_YEAR ¶12
-
int
SATURDAY ¶6
-
int
SECONDS_PER_MINUTE ¶60
-
int
SUNDAY ¶7
-
int
THURSDAY ¶4
-
int
TUESDAY ¶2
-
int
WEDNESDAY ¶3
-
int
WEEKS_PER_YEAR ¶52
-
int
YEARS_PER_CENTURY ¶100
-
int
YEARS_PER_DECADE ¶10
Property Summary
-
$age public @property-read
int
does a diffInYears() with default parameters
-
$day public @property-read
int<1, 31>
-
$dayOfWeek public @property-read
int<1, 7>
1 (for Monday) through 7 (for Sunday)
-
$dayOfYear public @property-read
int<0, 365>
0 through 365
-
$days protected static
array
Names of days of the week.
-
$daysInMonth public @property-read
int<1, 31>
number of days in the given month
-
$diffFormatter protected static
Cake\Chronos\DifferenceFormatterInterface|null
Instance of the diff formatting object.
-
$dst public @property-read
bool
daylight savings time indicator, true if DST, false otherwise
-
$half public @property-read
int<1, 2>
the half of the year, with 1 for months Jan...Jun and 2 for Jul...Dec.
-
$hour public @property-read
int<0, 23>
-
$lastErrors protected static
array|false
Errors from last time createFromFormat() was called.
-
$local public @property-read
bool
checks if the timezone is local, true if local, false otherwise
-
$micro public @property-read
int<0, 999999>
-
$microsecond public @property-read
int<0, 999999>
-
$minute public @property-read
int<0, 59>
-
$month public @property-read
int<1, 12>
-
$offset public @property-read
int
the timezone offset in seconds from UTC
-
$offsetHours public @property-read
int
the timezone offset in hours from UTC
-
$quarter public @property-read
int<1, 4>
the quarter of this instance, 1 - 4
-
$relativePattern protected static
string
Regex for relative period.
-
$second public @property-read
int<0, 59>
-
$testNow protected static
Cake\Chronos\Chronos|null
A test Chronos instance to be returned when now instances are created
-
$timestamp public @property-read
int
seconds since the Unix Epoch
-
$timezone public @property-read
DateTimeZone
the current timezone
-
$timezoneName public @property-read
string
-
$toStringFormat protected static
string
Format to use for __toString method when type juggling occurs.
-
$tz public @property-read
DateTimeZone
alias of timezone
-
$tzName public @property-read
string
-
$utc public @property-read
bool
checks if the timezone is UTC, true if UTC, false otherwise
-
$weekEndsAt protected static
int
Last day of week
-
$weekOfMonth public @property-read
int<1, 5>
1 through 5
-
$weekOfYear public @property-read
int<1, 53>
ISO-8601 week number of year, weeks starting on Monday
-
$weekStartsAt protected static
int
First day of week
-
$weekendDays protected static
array
Days of weekend
-
$year public @property-read
int
-
$yearIso public @property-read
int
Method Summary
-
__construct() public
Create a new Chronos instance.
-
__debugInfo() public
Return properties for debugging.
-
__get() public
Get a part of the object
-
__isset() public
Check if an attribute exists on the object
-
__toString() public
Returns a formatted string specified by
setToStringFormat()
or the defaultDEFAULT_TO_STRING_FORMAT
format. -
addDays() public
Add days to the instance. Positive $value travels forward while negative $value travels into the past.
-
addHours() public
Add hours to the instance. Positive $value travels forward while negative $value travels into the past.
-
addMinutes() public
Add minutes to the instance. Positive $value travels forward while negative $value travels into the past.
-
addMonths() public
Add months to the instance. Positive $value travels forward while negative $value travels into the past.
-
addMonthsWithOverflow() public
Add months with overflowing to the instance. Positive $value travels forward while negative $value travels into the past.
-
addSeconds() public
Add seconds to the instance. Positive $value travels forward while negative $value travels into the past.
-
addWeekdays() public
Add weekdays to the instance. Positive $value travels forward while negative $value travels into the past.
-
addWeeks() public
Add weeks to the instance. Positive $value travels forward while negative $value travels into the past.
-
addYears() public
Add years to the instance. Positive $value travel forward while negative $value travel into the past.
-
addYearsWithOverflow() public
Add years with overflowing to the instance. Positive $value travels forward while negative $value travels into the past.
-
average() public
Modify the current instance to the average of a given instance (default now) and the current instance.
-
between() public
Determines if the instance is between two others
-
closest() public
Get the closest date from the instance.
-
create() public static
Create an instance from a specific date and time.
-
createFromArray() public static
Creates an instance from an array of date and time values.
-
createFromDate() public static
Create an instance from just a date. The time portion is set to now.
-
createFromFormat() public static
Create an instance from a specific format
-
createFromTime() public static
Create an instance from just a time. The date portion is set to today.
-
createFromTimestamp() public static
Create an instance from a timestamp
-
createInterval() public static
Create a new DateInterval instance from specified values.
-
day() public
Set the instance's day
-
diff() public
Returns the difference between this instance and target.
-
diffFiltered() public
Get the difference by the given interval using a filter callable
-
diffForHumans() public
Get the difference in a human readable format.
-
diffFormatter() public static
Get the difference formatter instance or overwrite the current one.
-
diffInDays() public
Get the difference in days
-
diffInDaysFiltered() public
Get the difference in days using a filter callable
-
diffInHours() public
Get the difference in hours
-
diffInHoursFiltered() public
Get the difference in hours using a filter callable
-
diffInMinutes() public
Get the difference in minutes
-
diffInMonths() public
Get the difference in months
-
diffInMonthsIgnoreTimezone() public
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() public
Get the difference in seconds
-
diffInWeekdays() public
Get the difference in weekdays
-
diffInWeekendDays() public
Get the difference in weekend days using a filter
-
diffInWeeks() public
Get the difference in weeks
-
diffInYears() public
Get the difference in years
-
endOfCentury() public
Sets the date to end of the century and time to 23:59:59
-
endOfDay() public
Sets the time to 23:59:59 or 23:59:59.999999 if
$microseconds
is true. -
endOfDecade() public
Sets the date to end of the decade and time to 23:59:59
-
endOfMonth() public
Sets the date to end of the month and time to 23:59:59
-
endOfWeek() public
Sets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
-
endOfYear() public
Sets the date to end of the year and time to 23:59:59
-
equals() public
Determines if the instance is equal to another
-
farthest() public
Get the farthest date from the instance.
-
firstOfMonth() public
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. Chronos::MONDAY.
-
firstOfQuarter() public
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. Chronos::MONDAY.
-
firstOfYear() public
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. Chronos::MONDAY.
-
format() public
Returns formatted date string according to DateTimeImmutable::format().
-
fromNow() public static
Convenience method for getting the remaining time from a given time.
-
getLastErrors() public static
Returns parse warnings and errors from the last
createFromFormat()
call. -
getOffset() public
Returns the timezone offset.
-
getTestNow() public static
Get the Chronos instance (real or mock) to be returned when a "now" instance is created.
-
getTimestamp() public
Gets the Unix timestamp for this instance.
-
getTimezone() public
Return time zone set for this instance.
-
getWeekEndsAt() public static
Get the last day of week
-
getWeekStartsAt() public static
Get the first day of week
-
getWeekendDays() public static
Get weekend days
-
greaterThan() public
Determines if the instance is greater (after) than another
-
greaterThanOrEquals() public
Determines if the instance is greater (after) than or equal to another
-
hasRelativeKeywords() public static
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() public static
Determine if there is a valid test instance set. A valid test instance is anything that is not null.
-
hour() public
Set the instance's hour
-
instance() public static
Create an instance from a DateTimeInterface
-
isBirthday() public
Check if its the birthday. Compares the date/month values of the two dates.
-
isFirstHalf() public
Determines if the instance is within the first half of year
-
isFriday() public
Checks if this day is a Friday.
-
isFuture() public
Determines if the instance is in the future, ie. greater (after) than now
-
isLastMonth() public
Determines if the instance is within the last month
-
isLastWeek() public
Determines if the instance is within the last week
-
isLastYear() public
Determines if the instance is within the last year
-
isLeapYear() public
Determines if the instance is a leap year
-
isMonday() public
Checks if this day is a Monday.
-
isNextMonth() public
Determines if the instance is within the next month
-
isNextWeek() public
Determines if the instance is within the next week
-
isNextYear() public
Determines if the instance is within the next year
-
isPast() public
Determines if the instance is in the past, ie. less (before) than now
-
isSameDay() public
Checks if the passed in date is the same day as the instance current day.
-
isSameMonth() public
Returns whether the passed in date is the same month and year.
-
isSameYear() public
Returns whether passed in date is the same year.
-
isSaturday() public
Checks if this day is a Saturday.
-
isSecondHalf() public
Determines if the instance is within the second half of year
-
isSunday() public
Checks if this day is a Sunday.
-
isThisMonth() public
Returns true if this object represents a date within the current month
-
isThisWeek() public
Returns true if this object represents a date within the current week
-
isThisYear() public
Returns true if this object represents a date within the current year
-
isThursday() public
Checks if this day is a Thursday.
-
isToday() public
Determines if the instance is today
-
isTomorrow() public
Determines if the instance is tomorrow
-
isTuesday() public
Checks if this day is a Tuesday.
-
isWednesday() public
Checks if this day is a Wednesday.
-
isWeekday() public
Determines if the instance is a weekday
-
isWeekend() public
Determines if the instance is a weekend day
-
isWithinNext() public
Returns true this instance will happen within the specified interval
-
isYesterday() public
Determines if the instance is yesterday
-
lastOfMonth() public
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. Chronos::MONDAY.
-
lastOfQuarter() public
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. Chronos::MONDAY.
-
lastOfYear() public
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. Chronos::MONDAY.
-
lessThan() public
Determines if the instance is less (before) than another
-
lessThanOrEquals() public
Determines if the instance is less (before) or equal to another
-
max() public
Get the maximum instance between a given instance (default now) and the current instance.
-
maxValue() public static
Create an instance for the greatest supported date.
-
microsecond() public
Set the instance's microsecond
-
min() public
Get the minimum instance between a given instance (default now) and the current instance.
-
minValue() public static
Create an instance for the lowest supported date.
-
minute() public
Set the instance's minute
-
modify() public
Creates a new instance with date modified according to DateTimeImmutable::modifier().
-
month() public
Set the instance's month
-
next() public
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. Chronos::MONDAY.
-
notEquals() public
Determines if the instance is not equal to another
-
now() public static
Get an instance for the current date and time
-
nthOfMonth() public
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. Chronos::MONDAY.
-
nthOfQuarter() public
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. Chronos::MONDAY.
-
nthOfYear() public
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. Chronos::MONDAY.
-
parse() public static
Create an instance from a string. This is an alias for the constructor that allows better fluent syntax as it allows you to do Chronos::parse('Monday next week')->fn() rather than (new Chronos('Monday next week'))->fn()
-
previous() public
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. Chronos::MONDAY.
-
resetToStringFormat() public static
Resets the __toString() format to
DEFAULT_TO_STRING_FORMAT
. -
rolloverTime() protected static
Updates value to remaininger and returns rollover value for time unit or null if no rollover.
-
safeCreateDateTimeZone() protected static
Creates a DateTimeZone from a string or a DateTimeZone
-
second() public
Set the instance's second
-
secondsSinceMidnight() public
The number of seconds since midnight.
-
secondsUntilEndOfDay() public
The number of seconds until 23:59:59.
-
setDate() public
Sets the date.
-
setDateTime() public
Sets the date and time.
-
setISODate() public
Sets the date according to the ISO 8601 standard
-
setTestNow() public static
Set a Chronos 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. Chronos::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 Chronos::parse()
-
setTime() public
Sets the time.
-
setTimeFromTimeString() public
Set the time by time string
-
setTimestamp() public
Sets the date and time based on a Unix timestamp.
-
setTimezone() public
Set the instance's timezone from a string or object
-
setToStringFormat() public static
Sets the __toString() format.
-
setWeekEndsAt() public static
Set the last day of week
-
setWeekStartsAt() public static
Set the first day of week
-
setWeekendDays() public static
Set weekend days
-
startOfCentury() public
Sets the date to the first day of the century and the time to 00:00:00
-
startOfDay() public
Sets the time to 00:00:00
-
startOfDecade() public
Sets the date to the first day of the decade and the time to 00:00:00
-
startOfMonth() public
Sets the date to the first day of the month and the time to 00:00:00
-
startOfWeek() public
Sets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
-
startOfYear() public
Sets the date to the first day of the year and the time to 00:00:00
-
subDays() public
Remove days from the instance
-
subHours() public
Remove hours from the instance
-
subMinutes() public
Remove minutes from the instance
-
subMonths() public
Remove months from the instance
-
subMonthsWithOverflow() public
Add months with overflowing to the instance. Positive $value travels forward while negative $value travels into the past.
-
subSeconds() public
Remove seconds from the instance
-
subWeekdays() public
Remove weekdays from the instance
-
subWeeks() public
Remove weeks to the instance
-
subYears() public
Remove years from the instance.
-
subYearsWithOverflow() public
Remove years with overflow from the instance
-
timestamp() public
Set the instance's timestamp
-
toAtomString() public
Format the instance as ATOM
-
toCookieString() public
Format the instance as COOKIE
-
toDateString() public
Format the instance as date
-
toDateTimeString() public
Format the instance as date and time
-
toDayDateTimeString() public
Format the instance with day, date and time
-
toFormattedDateString() public
Format the instance as a readable date
-
toIso8601String() public
Format the instance as ISO8601
-
toNative() public
Returns a DateTimeImmutable instance
-
toQuarter() public
Returns the quarter
-
toRfc1036String() public
Format the instance as RFC1036
-
toRfc1123String() public
Format the instance as RFC1123
-
toRfc2822String() public
Format the instance as RFC2822
-
toRfc3339String() public
Format the instance as RFC3339
-
toRfc822String() public
Format the instance as RFC822
-
toRfc850String() public
Format the instance as RFC850
-
toRssString() public
Format the instance as RSS
-
toTimeString() public
Format the instance as time
-
toUnixString() public
Returns a UNIX timestamp.
-
toW3cString() public
Format the instance as W3C
-
toWeek() public
Returns ISO 8601 week number of year, weeks starting on Monday
-
today() public static
Create an instance for today
-
tomorrow() public static
Create an instance for tomorrow
-
wasWithinLast() public
Returns true this instance happened within the specified interval
-
year() public
Set the instance's year
-
yesterday() public static
Create an instance for yesterday
Method Detail
__construct() ¶ public
__construct(Cake\Chronos\ChronosDateCake\Chronos\ChronosTimeDateTimeInterface|string|int|null $time = 'now', DateTimeZone|string|null $timezone = null)
Create a new Chronos instance.
Please see the testing aids section (specifically static::setTestNow()) for more on the possibility of this constructor returning a test instance.
Parameters
-
Cake\Chronos\ChronosDateCake\Chronos\ChronosTimeDateTimeInterface|string|int|null
$time optional Fixed or relative time
-
DateTimeZone|string|null
$timezone optional The timezone for the instance
__get() ¶ public
__get(string $name): DateTimeZone|string|float|int|bool
Get a part of the object
Parameters
-
string
$name The property name to read.
Returns
DateTimeZone|string|float|int|bool
Throws
InvalidArgumentException
__isset() ¶ public
__isset(string $name): bool
Check if an attribute exists on the object
Parameters
-
string
$name The property name to check.
Returns
bool
__toString() ¶ public
__toString(): string
Returns a formatted string specified by setToStringFormat()
or the default DEFAULT_TO_STRING_FORMAT
format.
Returns
string
addDays() ¶ public
addDays(int $value): static
Add days to the instance. Positive $value travels forward while negative $value travels into the past.
Parameters
-
int
$value The number of days to add.
Returns
static
addHours() ¶ public
addHours(int $value): static
Add hours to the instance. Positive $value travels forward while negative $value travels into the past.
Parameters
-
int
$value The number of hours to add.
Returns
static
addMinutes() ¶ public
addMinutes(int $value): static
Add minutes to the instance. Positive $value travels forward while negative $value travels into the past.
Parameters
-
int
$value The number of minutes to add.
Returns
static
addMonths() ¶ public
addMonths(int $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
-
int
$value The number of months to add.
Returns
static
addMonthsWithOverflow() ¶ public
addMonthsWithOverflow(int $value): static
Add months with overflowing to the instance. Positive $value travels forward while negative $value travels into the past.
If the new ChronosDate does not exist, the days overflow into the next month.
Example:
(new Chronos('2012-01-30'))->addMonthsWithOverflow(1); // Results in 2013-03-01
Parameters
-
int
$value The number of months to add.
Returns
static
addSeconds() ¶ public
addSeconds(int $value): static
Add seconds to the instance. Positive $value travels forward while negative $value travels into the past.
Parameters
-
int
$value The number of seconds to add.
Returns
static
addWeekdays() ¶ public
addWeekdays(int $value): static
Add weekdays to the instance. Positive $value travels forward while negative $value travels into the past.
Parameters
-
int
$value The number of weekdays to add.
Returns
static
addWeeks() ¶ public
addWeeks(int $value): static
Add weeks to the instance. Positive $value travels forward while negative $value travels into the past.
Parameters
-
int
$value The number of weeks to add.
Returns
static
addYears() ¶ public
addYears(int $value): static
Add years to the instance. Positive $value travel forward while negative $value travel into the past.
If the new ChronosDate 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
-
int
$value The number of years to add.
Returns
static
addYearsWithOverflow() ¶ public
addYearsWithOverflow(int $value): static
Add years with overflowing to the instance. Positive $value travels forward while negative $value travels into the past.
If the new ChronosDate does not exist, the days overflow into the next month.
Example:
(new Chronos('2012-02-29'))->addYearsWithOverflow(1); // Results in 2013-03-01
Parameters
-
int
$value The number of years to add.
Returns
static
average() ¶ public
average(DateTimeInterface|null $other = null): static
Modify the current instance to the average of a given instance (default now) and the current instance.
Parameters
-
DateTimeInterface|null
$other optional The instance to compare with.
Returns
static
between() ¶ public
between(DateTimeInterface $start, DateTimeInterface $end, bool $equals = true): bool
Determines if the instance is between two others
Parameters
-
DateTimeInterface
$start Start of target range
-
DateTimeInterface
$end End of target range
-
bool
$equals optional Whether to include the beginning and end of range
Returns
bool
closest() ¶ public
closest(DateTimeInterface $first, DateTimeInterface $second, DateTimeInterface ...$others): static
Get the closest date from the instance.
Parameters
-
DateTimeInterface
$first The instance to compare with.
-
DateTimeInterface
$second The instance to compare with.
-
DateTimeInterface
...$others Others instances to compare with.
Returns
static
create() ¶ 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 $timezone = null): static
Create an 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, $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
-
int|null
$year optional The year to create an instance with.
-
int|null
$month optional The month to create an instance with.
-
int|null
$day optional The day to create an instance with.
-
int|null
$hour optional The hour to create an instance with.
-
int|null
$minute optional The minute to create an instance with.
-
int|null
$second optional The second to create an instance with.
-
int|null
$microsecond optional The microsecond to create an instance with.
-
DateTimeZone|string|null
$timezone optional The DateTimeZone object or timezone name the new instance should use.
Returns
static
createFromArray() ¶ public static
createFromArray(array<int|string> $values): static
Creates an instance from an array of date and time values.
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
-
array<int|string>
$values Array of date and time values.
Returns
static
createFromDate() ¶ public static
createFromDate(int|null $year = null, int|null $month = null, int|null $day = null, DateTimeZone|string|null $timezone = null): static
Create an instance from just a date. The time portion is set to now.
Parameters
-
int|null
$year optional The year to create an instance with.
-
int|null
$month optional The month to create an instance with.
-
int|null
$day optional The day to create an instance with.
-
DateTimeZone|string|null
$timezone optional The DateTimeZone object or timezone name the new instance should use.
Returns
static
createFromFormat() ¶ public static
createFromFormat(string $format, string $time, DateTimeZone|string|null $timezone = null): static
Create an instance from a specific format
Parameters
-
string
$format The date() compatible format string.
-
string
$time The formatted date string to interpret.
-
DateTimeZone|string|null
$timezone optional The DateTimeZone object or timezone name the new instance should use.
Returns
static
Throws
InvalidArgumentException
createFromTime() ¶ public static
createFromTime(int|null $hour = null, int|null $minute = null, int|null $second = null, int|null $microsecond = null, DateTimeZone|string|null $timezone = null): static
Create an instance from just a time. The date portion is set to today.
Parameters
-
int|null
$hour optional The hour to create an instance with.
-
int|null
$minute optional The minute to create an instance with.
-
int|null
$second optional The second to create an instance with.
-
int|null
$microsecond optional The microsecond to create an instance with.
-
DateTimeZone|string|null
$timezone optional The DateTimeZone object or timezone name the new instance should use.
Returns
static
createFromTimestamp() ¶ public static
createFromTimestamp(float|int $timestamp, DateTimeZone|string|null $timezone = null): static
Create an instance from a timestamp
Parameters
-
float|int
$timestamp The timestamp to create an instance from.
-
DateTimeZone|string|null
$timezone optional The DateTimeZone object or timezone name the new instance should use.
Returns
static
createInterval() ¶ 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
Create a new DateInterval instance from specified values.
Parameters
-
int|null
$years optional The year to use.
-
int|null
$months optional The month to use.
-
int|null
$weeks optional The week to use.
-
int|null
$days optional The day to use.
-
int|null
$hours optional The hours to use.
-
int|null
$minutes optional The minutes to use.
-
int|null
$seconds optional The seconds to use.
-
int|null
$microseconds optional The microseconds to use.
Returns
DateInterval
day() ¶ public
day(int $value): static
Set the instance's day
Parameters
-
int
$value The day value.
Returns
static
diff() ¶ public
diff(DateTimeInterface $target, bool $absolute = false): DateInterval
Returns the difference between this instance and target.
Parameters
-
DateTimeInterface
$target Target instance
-
bool
$absolute optional Whether the interval is forced to be positive
Returns
DateInterval
diffFiltered() ¶ public
diffFiltered(DateInterval $interval, callable $callback, DateTimeInterface|null $other = null, bool $absolute = true, int $options = 0): int
Get the difference by the given interval using a filter callable
Parameters
-
DateInterval
$interval An interval to traverse by
-
callable
$callback The callback to use for filtering.
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
-
int
$options optional DatePeriod options, {@see https://www.php.net/manual/en/class.dateperiod.php}
Returns
int
diffForHumans() ¶ public
diffForHumans(DateTimeInterface|null $other = null, bool $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
-
DateTimeInterface|null
$other optional The datetime to compare with.
-
bool
$absolute optional removes time difference modifiers ago, after, etc
Returns
string
diffFormatter() ¶ public static
diffFormatter(Cake\Chronos\DifferenceFormatterInterface|null $formatter = null): Cake\Chronos\DifferenceFormatterInterface
Get the difference formatter instance or overwrite the current one.
Parameters
-
Cake\Chronos\DifferenceFormatterInterface|null
$formatter optional The formatter instance when setting.
Returns
Cake\Chronos\DifferenceFormatterInterface
diffInDays() ¶ public
diffInDays(DateTimeInterface|null $other = null, bool $absolute = true): int
Get the difference in days
Parameters
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
diffInDaysFiltered() ¶ public
diffInDaysFiltered(callable $callback, DateTimeInterface|null $other = null, bool $absolute = true, int $options = 0): int
Get the difference in days using a filter callable
Parameters
-
callable
$callback The callback to use for filtering.
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
-
int
$options optional DatePeriod options, {@see https://www.php.net/manual/en/class.dateperiod.php}
Returns
int
diffInHours() ¶ public
diffInHours(DateTimeInterface|null $other = null, bool $absolute = true): int
Get the difference in hours
Parameters
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
diffInHoursFiltered() ¶ public
diffInHoursFiltered(callable $callback, DateTimeInterface|null $other = null, bool $absolute = true, int $options = 0): int
Get the difference in hours using a filter callable
Parameters
-
callable
$callback The callback to use for filtering.
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
-
int
$options optional DatePeriod options, {@see https://www.php.net/manual/en/class.dateperiod.php}
Returns
int
diffInMinutes() ¶ public
diffInMinutes(DateTimeInterface|null $other = null, bool $absolute = true): int
Get the difference in minutes
Parameters
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
diffInMonths() ¶ public
diffInMonths(DateTimeInterface|null $other = null, bool $absolute = true): int
Get the difference in months
Parameters
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
diffInMonthsIgnoreTimezone() ¶ public
diffInMonthsIgnoreTimezone(DateTimeInterface|null $other = null, bool $absolute = true): 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.
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
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
diffInSeconds() ¶ public
diffInSeconds(DateTimeInterface|null $other = null, bool $absolute = true): int
Get the difference in seconds
Parameters
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
diffInWeekdays() ¶ public
diffInWeekdays(DateTimeInterface|null $other = null, bool $absolute = true, int $options = 0): int
Get the difference in weekdays
Parameters
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
-
int
$options optional DatePeriod options, {@see https://www.php.net/manual/en/class.dateperiod.php}
Returns
int
diffInWeekendDays() ¶ public
diffInWeekendDays(DateTimeInterface|null $other = null, bool $absolute = true, int $options = 0): int
Get the difference in weekend days using a filter
Parameters
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
-
int
$options optional DatePeriod options, {@see https://www.php.net/manual/en/class.dateperiod.php}
Returns
int
diffInWeeks() ¶ public
diffInWeeks(DateTimeInterface|null $other = null, bool $absolute = true): int
Get the difference in weeks
Parameters
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
diffInYears() ¶ public
diffInYears(DateTimeInterface|null $other = null, bool $absolute = true): int
Get the difference in years
Parameters
-
DateTimeInterface|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
endOfCentury() ¶ public
endOfCentury(): static
Sets the date to end of the century and time to 23:59:59
Returns
static
endOfDay() ¶ public
endOfDay(bool $microseconds = false): static
Sets the time to 23:59:59 or 23:59:59.999999
if $microseconds
is true.
Parameters
-
bool
$microseconds optional Whether to set microseconds
Returns
static
endOfDecade() ¶ public
endOfDecade(): static
Sets the date to end of the decade and time to 23:59:59
Returns
static
endOfMonth() ¶ public
endOfMonth(): static
Sets the date to end of the month and time to 23:59:59
Returns
static
endOfWeek() ¶ public
endOfWeek(): static
Sets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
Returns
static
endOfYear() ¶ public
endOfYear(): static
Sets the date to end of the year and time to 23:59:59
Returns
static
equals() ¶ public
equals(DateTimeInterface $other): bool
Determines if the instance is equal to another
Parameters
-
DateTimeInterface
$other The instance to compare with.
Returns
bool
farthest() ¶ public
farthest(DateTimeInterface $first, DateTimeInterface $second, DateTimeInterface ...$others): static
Get the farthest date from the instance.
Parameters
-
DateTimeInterface
$first The instance to compare with.
-
DateTimeInterface
$second The instance to compare with.
-
DateTimeInterface
...$others Others instances to compare with.
Returns
static
firstOfMonth() ¶ public
firstOfMonth(int|null $dayOfWeek = null): 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. Chronos::MONDAY.
Parameters
-
int|null
$dayOfWeek optional The day of the week to move to.
Returns
static
firstOfQuarter() ¶ public
firstOfQuarter(int|null $dayOfWeek = null): 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. Chronos::MONDAY.
Parameters
-
int|null
$dayOfWeek optional The day of the week to move to.
Returns
static
firstOfYear() ¶ public
firstOfYear(int|null $dayOfWeek = null): 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. Chronos::MONDAY.
Parameters
-
int|null
$dayOfWeek optional The day of the week to move to.
Returns
static
format() ¶ public
format(string $format): string
Returns formatted date string according to DateTimeImmutable::format().
Parameters
-
string
$format String format
Returns
string
fromNow() ¶ public static
fromNow(DateTimeInterface $other): DateInterval|bool
Convenience method for getting the remaining time from a given time.
Parameters
-
DateTimeInterface
$other The date to get the remaining time from.
Returns
DateInterval|bool
getLastErrors() ¶ public static
getLastErrors(): array|false
Returns parse warnings and errors from the last createFromFormat()
call.
Returns the same data as DateTimeImmutable::getLastErrors().
Returns
array|false
getTestNow() ¶ public static
getTestNow(): Cake\Chronos\Chronos|null
Get the Chronos instance (real or mock) to be returned when a "now" instance is created.
Returns
Cake\Chronos\Chronos|null
getTimezone() ¶ public
getTimezone(): DateTimeZone
Return time zone set for this instance.
Returns
DateTimeZone
greaterThan() ¶ public
greaterThan(DateTimeInterface $other): bool
Determines if the instance is greater (after) than another
Parameters
-
DateTimeInterface
$other The instance to compare with.
Returns
bool
greaterThanOrEquals() ¶ public
greaterThanOrEquals(DateTimeInterface $other): bool
Determines if the instance is greater (after) than or equal to another
Parameters
-
DateTimeInterface
$other The instance to compare with.
Returns
bool
hasRelativeKeywords() ¶ public static
hasRelativeKeywords(string|null $time): 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
Parameters
-
string|null
$time The time string to check.
Returns
bool
hasTestNow() ¶ public static
hasTestNow(): bool
Determine if there is a valid test instance set. A valid test instance is anything that is not null.
Returns
bool
hour() ¶ public
hour(int $value): static
Set the instance's hour
Parameters
-
int
$value The hour value.
Returns
static
instance() ¶ public static
instance(DateTimeInterface $other): static
Create an instance from a DateTimeInterface
Parameters
-
DateTimeInterface
$other The datetime instance to convert.
Returns
static
isBirthday() ¶ public
isBirthday(DateTimeInterface|null $other = null): bool
Check if its the birthday. Compares the date/month values of the two dates.
Parameters
-
DateTimeInterface|null
$other optional The instance to compare with or null to use current day.
Returns
bool
isFirstHalf() ¶ public
isFirstHalf(): bool
Determines if the instance is within the first half of year
Returns
bool
isFuture() ¶ public
isFuture(): bool
Determines if the instance is in the future, ie. greater (after) than now
Returns
bool
isLastMonth() ¶ public
isLastMonth(): bool
Determines if the instance is within the last month
Returns
bool
isLastWeek() ¶ public
isLastWeek(): bool
Determines if the instance is within the last week
Returns
bool
isLastYear() ¶ public
isLastYear(): bool
Determines if the instance is within the last year
Returns
bool
isNextMonth() ¶ public
isNextMonth(): bool
Determines if the instance is within the next month
Returns
bool
isNextWeek() ¶ public
isNextWeek(): bool
Determines if the instance is within the next week
Returns
bool
isNextYear() ¶ public
isNextYear(): bool
Determines if the instance is within the next year
Returns
bool
isPast() ¶ public
isPast(): bool
Determines if the instance is in the past, ie. less (before) than now
Returns
bool
isSameDay() ¶ public
isSameDay(DateTimeInterface $other): bool
Checks if the passed in date is the same day as the instance current day.
Parameters
-
DateTimeInterface
$other The instance to check against.
Returns
bool
isSameMonth() ¶ public
isSameMonth(DateTimeInterface $other): bool
Returns whether the passed in date is the same month and year.
Parameters
-
DateTimeInterface
$other The instance to check against.
Returns
bool
isSameYear() ¶ public
isSameYear(DateTimeInterface $other): bool
Returns whether passed in date is the same year.
Parameters
-
DateTimeInterface
$other The instance to check against.
Returns
bool
isSecondHalf() ¶ public
isSecondHalf(): bool
Determines if the instance is within the second half of year
Returns
bool
isThisMonth() ¶ public
isThisMonth(): bool
Returns true if this object represents a date within the current month
Returns
bool
isThisWeek() ¶ public
isThisWeek(): bool
Returns true if this object represents a date within the current week
Returns
bool
isThisYear() ¶ public
isThisYear(): bool
Returns true if this object represents a date within the current year
Returns
bool
isWithinNext() ¶ public
isWithinNext(string|int $timeInterval): bool
Returns true this instance will happen within the specified interval
Parameters
-
string|int
$timeInterval the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.
Returns
bool
lastOfMonth() ¶ public
lastOfMonth(int|null $dayOfWeek = null): 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. Chronos::MONDAY.
Parameters
-
int|null
$dayOfWeek optional The day of the week to move to.
Returns
static
lastOfQuarter() ¶ public
lastOfQuarter(int|null $dayOfWeek = null): 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. Chronos::MONDAY.
Parameters
-
int|null
$dayOfWeek optional The day of the week to move to.
Returns
static
lastOfYear() ¶ public
lastOfYear(int|null $dayOfWeek = null): 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. Chronos::MONDAY.
Parameters
-
int|null
$dayOfWeek optional The day of the week to move to.
Returns
static
lessThan() ¶ public
lessThan(DateTimeInterface $other): bool
Determines if the instance is less (before) than another
Parameters
-
DateTimeInterface
$other The instance to compare with.
Returns
bool
lessThanOrEquals() ¶ public
lessThanOrEquals(DateTimeInterface $other): bool
Determines if the instance is less (before) or equal to another
Parameters
-
DateTimeInterface
$other The instance to compare with.
Returns
bool
max() ¶ public
max(DateTimeInterface|null $other = null): static
Get the maximum instance between a given instance (default now) and the current instance.
Parameters
-
DateTimeInterface|null
$other optional The instance to compare with.
Returns
static
maxValue() ¶ public static
maxValue(): static
Create an instance for the greatest supported date.
Returns
static
microsecond() ¶ public
microsecond(int $value): static
Set the instance's microsecond
Parameters
-
int
$value The microsecond value.
Returns
static
min() ¶ public
min(DateTimeInterface|null $other = null): static
Get the minimum instance between a given instance (default now) and the current instance.
Parameters
-
DateTimeInterface|null
$other optional The instance to compare with.
Returns
static
minValue() ¶ public static
minValue(): static
Create an instance for the lowest supported date.
Returns
static
minute() ¶ public
minute(int $value): static
Set the instance's minute
Parameters
-
int
$value The minute value.
Returns
static
modify() ¶ public
modify(string $modifier): static
Creates a new instance with date modified according to DateTimeImmutable::modifier().
Parameters
-
string
$modifier Date modifier
Returns
static
Throws
InvalidArgumentException
See Also
month() ¶ public
month(int $value): static
Set the instance's month
Parameters
-
int
$value The month value.
Returns
static
next() ¶ public
next(int|null $dayOfWeek = null): static
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. Chronos::MONDAY.
Parameters
-
int|null
$dayOfWeek optional The day of the week to move to.
Returns
static
notEquals() ¶ public
notEquals(DateTimeInterface $other): bool
Determines if the instance is not equal to another
Parameters
-
DateTimeInterface
$other The instance to compare with.
Returns
bool
now() ¶ public static
now(DateTimeZone|string|null $timezone = null): static
Get an instance for the current date and time
Parameters
-
DateTimeZone|string|null
$timezone optional The DateTimeZone object or timezone name.
Returns
static
nthOfMonth() ¶ public
nthOfMonth(int $nth, int $dayOfWeek): 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. Use the supplied consts to indicate the desired dayOfWeek, ex. Chronos::MONDAY.
Parameters
-
int
$nth The offset to use.
-
int
$dayOfWeek The day of the week to move to.
Returns
static|false
nthOfQuarter() ¶ public
nthOfQuarter(int $nth, int $dayOfWeek): 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. Use the supplied consts to indicate the desired dayOfWeek, ex. Chronos::MONDAY.
Parameters
-
int
$nth The offset to use.
-
int
$dayOfWeek The day of the week to move to.
Returns
static|false
nthOfYear() ¶ public
nthOfYear(int $nth, int $dayOfWeek): 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. Use the supplied consts to indicate the desired dayOfWeek, ex. Chronos::MONDAY.
Parameters
-
int
$nth The offset to use.
-
int
$dayOfWeek The day of the week to move to.
Returns
static|false
parse() ¶ public static
parse(Cake\Chronos\ChronosDateCake\Chronos\ChronosTimeDateTimeInterface|string|int|null $time = 'now', DateTimeZone|string|null $timezone = null): static
Create an instance from a string. This is an alias for the constructor that allows better fluent syntax as it allows you to do Chronos::parse('Monday next week')->fn() rather than (new Chronos('Monday next week'))->fn()
Parameters
-
Cake\Chronos\ChronosDateCake\Chronos\ChronosTimeDateTimeInterface|string|int|null
$time optional The strtotime compatible string to parse
-
DateTimeZone|string|null
$timezone optional The DateTimeZone object or timezone name.
Returns
static
previous() ¶ public
previous(int|null $dayOfWeek = null): static
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. Chronos::MONDAY.
Parameters
-
int|null
$dayOfWeek optional The day of the week to move to.
Returns
static
resetToStringFormat() ¶ public static
resetToStringFormat(): void
Resets the __toString() format to DEFAULT_TO_STRING_FORMAT
.
Returns
void
rolloverTime() ¶ protected static
rolloverTime(int|null $value, int $max): int|null
Updates value to remaininger and returns rollover value for time unit or null if no rollover.
Parameters
-
int|null
$value Time unit value
-
int
$max Time unit max value
Returns
int|null
safeCreateDateTimeZone() ¶ protected static
safeCreateDateTimeZone(DateTimeZone|string|null $object): DateTimeZone
Creates a DateTimeZone from a string or a DateTimeZone
Parameters
-
DateTimeZone|string|null
$object The value to convert.
Returns
DateTimeZone
Throws
InvalidArgumentException
second() ¶ public
second(int $value): static
Set the instance's second
Parameters
-
int
$value The seconds value.
Returns
static
secondsSinceMidnight() ¶ public
secondsSinceMidnight(): int
The number of seconds since midnight.
Returns
int
secondsUntilEndOfDay() ¶ public
secondsUntilEndOfDay(): int
The number of seconds until 23:59:59.
Returns
int
setDate() ¶ public
setDate(int $year, int $month, int $day): static
Sets the date.
Parameters
-
int
$year The year to set.
-
int
$month The month to set.
-
int
$day The day to set.
Returns
static
setDateTime() ¶ public
setDateTime(int $year, int $month, int $day, int $hour, int $minute, int $second = 0): static
Sets the date and time.
Parameters
-
int
$year The year to set.
-
int
$month The month to set.
-
int
$day The day to set.
-
int
$hour The hour to set.
-
int
$minute The minute to set.
-
int
$second optional The second to set.
Returns
static
setISODate() ¶ public
setISODate(int $year, int $week, int $dayOfWeek = 1): static
Sets the date according to the ISO 8601 standard
Parameters
-
int
$year Year of the date.
-
int
$week Week of the date.
-
int
$dayOfWeek optional Offset from the first day of the week.
Returns
static
setTestNow() ¶ public static
setTestNow(Cake\Chronos\Chronos|string|null $testNow = null): void
Set a Chronos 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. Chronos::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 Chronos::parse()
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
-
Cake\Chronos\Chronos|string|null
$testNow optional The instance to use for all future instances.
Returns
void
setTime() ¶ public
setTime(int $hours, int $minutes, int $seconds = 0, int $microseconds = 0): static
Sets the time.
Parameters
-
int
$hours Hours of the time
-
int
$minutes Minutes of the time
-
int
$seconds optional Seconds of the time
-
int
$microseconds optional Microseconds of the time
Returns
static
setTimeFromTimeString() ¶ public
setTimeFromTimeString(string $time): static
Set the time by time string
Parameters
-
string
$time Time as string.
Returns
static
setTimestamp() ¶ public
setTimestamp(int $timestamp): static
Sets the date and time based on a Unix timestamp.
Parameters
-
int
$timestamp Unix timestamp representing the date
Returns
static
setTimezone() ¶ public
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
setToStringFormat() ¶ public static
setToStringFormat(string $format): void
Sets the __toString() format.
Parameters
-
string
$format See
format()
for accepted specifiers.
Returns
void
setWeekEndsAt() ¶ public static
setWeekEndsAt(int $day): void
Set the last day of week
Parameters
-
int
$day The day the week ends with.
Returns
void
setWeekStartsAt() ¶ public static
setWeekStartsAt(int $day): void
Set the first day of week
Parameters
-
int
$day The day the week starts with.
Returns
void
setWeekendDays() ¶ public static
setWeekendDays(array $days): void
Set weekend days
Parameters
-
array
$days Which days are 'weekends'.
Returns
void
startOfCentury() ¶ public
startOfCentury(): static
Sets the date to the first day of the century and the time to 00:00:00
Returns
static
startOfDecade() ¶ public
startOfDecade(): static
Sets the date to the first day of the decade and the time to 00:00:00
Returns
static
startOfMonth() ¶ public
startOfMonth(): static
Sets the date to the first day of the month and the time to 00:00:00
Returns
static
startOfWeek() ¶ public
startOfWeek(): static
Sets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
Returns
static
startOfYear() ¶ public
startOfYear(): static
Sets the date to the first day of the year and the time to 00:00:00
Returns
static
subDays() ¶ public
subDays(int $value): static
Remove days from the instance
Parameters
-
int
$value The number of days to remove.
Returns
static
subHours() ¶ public
subHours(int $value): static
Remove hours from the instance
Parameters
-
int
$value The number of hours to remove.
Returns
static
subMinutes() ¶ public
subMinutes(int $value): static
Remove minutes from the instance
Parameters
-
int
$value The number of minutes to remove.
Returns
static
subMonths() ¶ public
subMonths(int $value): static
Remove months from the instance
Has the same behavior as addMonths()
.
Parameters
-
int
$value The number of months to remove.
Returns
static
subMonthsWithOverflow() ¶ public
subMonthsWithOverflow(int $value): static
Add months with overflowing to the instance. Positive $value travels forward while negative $value travels into the past.
If the new ChronosDate does not exist, the days overflow into the next month.
Example:
(new Chronos('2012-01-30'))->addMonthsWithOverflow(1); // Results in 2013-03-01
Parameters
-
int
$value The number of months to remove.
Returns
static
subSeconds() ¶ public
subSeconds(int $value): static
Remove seconds from the instance
Parameters
-
int
$value The number of seconds to remove.
Returns
static
subWeekdays() ¶ public
subWeekdays(int $value): static
Remove weekdays from the instance
Parameters
-
int
$value The number of weekdays to remove.
Returns
static
subWeeks() ¶ public
subWeeks(int $value): static
Remove weeks to the instance
Parameters
-
int
$value The number of weeks to remove.
Returns
static
subYears() ¶ public
subYears(int $value): static
Remove years from the instance.
Has the same behavior as addYears()
.
Parameters
-
int
$value The number of years to remove.
Returns
static
subYearsWithOverflow() ¶ public
subYearsWithOverflow(int $value): static
Remove years with overflow from the instance
Has the same behavior as addYeasrWithOverflow()
.
Parameters
-
int
$value The number of years to remove.
Returns
static
timestamp() ¶ public
timestamp(int $value): static
Set the instance's timestamp
Parameters
-
int
$value The timestamp value to set.
Returns
static
toDateTimeString() ¶ public
toDateTimeString(): string
Format the instance as date and time
Returns
string
toDayDateTimeString() ¶ public
toDayDateTimeString(): string
Format the instance with day, date and time
Returns
string
toFormattedDateString() ¶ public
toFormattedDateString(): string
Format the instance as a readable date
Returns
string
toNative() ¶ public
toNative(): DateTimeImmutable
Returns a DateTimeImmutable instance
This method returns a PHP DateTimeImmutable without Chronos extensions.
Returns
DateTimeImmutable
toQuarter() ¶ public
toQuarter(bool $range = false): array|int
Returns the quarter
Parameters
-
bool
$range optional Range.
Returns
array|int
toRfc1036String() ¶ public
toRfc1036String(): string
Format the instance as RFC1036
Returns
string
Links
toRfc1123String() ¶ public
toRfc1123String(): string
Format the instance as RFC1123
Returns
string
Links
toRfc2822String() ¶ public
toRfc2822String(): string
Format the instance as RFC2822
Returns
string
Links
toRfc3339String() ¶ public
toRfc3339String(): string
Format the instance as RFC3339
Returns
string
Links
toRfc822String() ¶ public
toRfc822String(): string
Format the instance as RFC822
Returns
string
Links
toRfc850String() ¶ public
toRfc850String(): string
Format the instance as RFC850
Returns
string
Links
toWeek() ¶ public
toWeek(): int
Returns ISO 8601 week number of year, weeks starting on Monday
Returns
int
today() ¶ public static
today(DateTimeZone|string|null $timezone = null): static
Create an instance for today
Parameters
-
DateTimeZone|string|null
$timezone optional The timezone to use.
Returns
static
tomorrow() ¶ public static
tomorrow(DateTimeZone|string|null $timezone = null): static
Create an instance for tomorrow
Parameters
-
DateTimeZone|string|null
$timezone optional The DateTimeZone object or timezone name the new instance should use.
Returns
static
wasWithinLast() ¶ public
wasWithinLast(string|int $timeInterval): bool
Returns true this instance happened within the specified interval
Parameters
-
string|int
$timeInterval the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute.
Returns
bool
year() ¶ public
year(int $value): static
Set the instance's year
Parameters
-
int
$value The year value.
Returns
static
yesterday() ¶ public static
yesterday(DateTimeZone|string|null $timezone = null): static
Create an instance for yesterday
Parameters
-
DateTimeZone|string|null
$timezone optional The DateTimeZone object or timezone name the new instance should use.
Returns
static
Property Detail
$diffFormatter ¶ protected static
Instance of the diff formatting object.
Type
Cake\Chronos\DifferenceFormatterInterface|null
$dst ¶ public @property-read
daylight savings time indicator, true if DST, false otherwise
Type
bool
$half ¶ public @property-read
the half of the year, with 1 for months Jan...Jun and 2 for Jul...Dec.
Type
int<1, 2>
$lastErrors ¶ protected static
Errors from last time createFromFormat() was called.
Type
array|false
$local ¶ public @property-read
checks if the timezone is local, true if local, false otherwise
Type
bool
$testNow ¶ protected static
A test Chronos instance to be returned when now instances are created
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.
Type
Cake\Chronos\Chronos|null
$toStringFormat ¶ protected static
Format to use for __toString method when type juggling occurs.
Type
string
$weekOfYear ¶ public @property-read
ISO-8601 week number of year, weeks starting on Monday
Type
int<1, 53>