Class ChronosDate
An immutable date object.
This class is useful when you want to represent a calendar date and ignore times. This means that timezone changes take no effect as a calendar date exists in all timezones in each respective date.
Constants
-
string
DEFAULT_TO_STRING_FORMAT ¶'Y-m-d'
Default format to use for __toString method when type juggling occurs.
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.
-
$half public @property-read
int<1, 2>
the half of the year, with 1 for months Jan...Jun and 2 for Jul...Dec.
-
$lastErrors protected static
array|false
Errors from last time createFromFormat() was called.
-
$month public @property-read
int<1, 12>
-
$native protected
DateTimeImmutable
-
$quarter public @property-read
int<1, 4>
the quarter of this instance, 1 - 4
-
$toStringFormat protected static
string
Format to use for __toString method when type juggling occurs.
-
$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
-
$year public @property-read
int
-
$yearIso public @property-read
int
Method Summary
-
__construct() public
Create a new Immutable Date 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. -
add() public
Add an Interval to a Date
-
addDays() public
Add days 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.
-
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.
-
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.
-
createFromArray() public static
Creates an instance from an array of date values.
-
createFromFormat() public static
Create an instance from a specific format
-
createNative() protected
Initializes the PHP DateTimeImmutable object.
-
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
-
diffInMonths() public
Get the difference in months
-
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
Resets the date to end of the century and time to 23:59:59
-
endOfDecade() public
Resets the date to end of the decade
-
endOfMonth() public
Resets the date to end of the month
-
endOfWeek() public
Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
-
endOfYear() public
Resets the date to end of the year
-
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().
-
getLastErrors() public static
Returns parse warnings and errors from the last
createFromFormat()
call. -
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
-
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
-
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.
-
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
-
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 today's date.
-
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
. -
setDate() public
Sets the date.
-
setISODate() public
Sets the date according to the ISO 8601 standard
-
setToStringFormat() public static
Sets the __toString() format.
-
startOfCentury() public
Resets the date to the first day of the century
-
startOfDecade() public
Resets the date to the first day of the decade
-
startOfMonth() public
Resets the date to the first day of the month
-
startOfWeek() public
Resets the date to the first day of week (defined in $weekStartsAt)
-
startOfYear() public
Resets the date to the first day of the year
-
sub() public
Subtract an Interval from a Date.
-
subDays() public
Remove days 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.
-
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
-
toAtomString() public
Format the instance as ATOM
-
toCookieString() public
Format the instance as COOKIE
-
toDateString() public
Format the instance as date
-
toDateTimeImmutable() public
Returns the date as a
DateTimeImmutable
instance at midnight. -
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 the date as a
DateTimeImmutable
instance at midnight. -
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
Get today's date.
-
tomorrow() public static
Get tomorrow's date.
-
wasWithinLast() public
Returns true this instance happened within the specified interval
-
year() public
Set the instance's year
-
yesterday() public static
Get yesterday's date.
Method Detail
__construct() ¶ public
__construct(Cake\Chronos\ChronosDateDateTimeInterface|string $time = 'now', DateTimeZone|string|null $timezone = null)
Create a new Immutable Date instance.
Dates do not have time or timezone components exposed. Internally ChronosDate wraps a PHP DateTimeImmutable but limits modifications to only those that operate on day values.
By default dates will be calculated from the server's default timezone.
You can use the timezone
parameter to use a different timezone. Timezones
are used when parsing relative date expressions like today
and yesterday
but do not participate in parsing values like 2022-01-01
.
Parameters
-
Cake\Chronos\ChronosDateDateTimeInterface|string
$time optional Fixed or relative time
-
DateTimeZone|string|null
$timezone optional The time zone used for 'now'
__get() ¶ public
__get(string $name): string|float|int|bool
Get a part of the object
Parameters
-
string
$name The property name to read.
Returns
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
add() ¶ public
add(DateInterval $interval): static
Add an Interval to a Date
Any changes to the time will be ignored and reset to 00:00:00
Parameters
-
DateInterval
$interval The interval to modify this date by.
Returns
static
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
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
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
between() ¶ public
between(Cake\Chronos\ChronosDate $start, Cake\Chronos\ChronosDate $end, bool $equals = true): bool
Determines if the instance is between two others
Parameters
-
Cake\Chronos\ChronosDate
$start Start of target range
-
Cake\Chronos\ChronosDate
$end End of target range
-
bool
$equals optional Whether to include the beginning and end of range
Returns
bool
closest() ¶ public
closest(Cake\Chronos\ChronosDate $first, Cake\Chronos\ChronosDate $second, Cake\Chronos\ChronosDate ...$others): self
Get the closest date from the instance.
Parameters
-
Cake\Chronos\ChronosDate
$first The instance to compare with.
-
Cake\Chronos\ChronosDate
$second The instance to compare with.
-
Cake\Chronos\ChronosDate
...$others Others instance to compare with.
Returns
self
create() ¶ public static
create(int $year, int $month, int $day): static
Create an instance from a specific date.
Parameters
-
int
$year The year to create an instance with.
-
int
$month The month to create an instance with.
-
int
$day The day to create an instance with.
Returns
static
createFromArray() ¶ public static
createFromArray(array<int|string> $values): static
Creates an instance from an array of date values.
Allowed values:
- year
- month
- day
Parameters
-
array<int|string>
$values Array of date and time values.
Returns
static
createFromFormat() ¶ public static
createFromFormat(string $format, string $time): static
Create an instance from a specific format
Parameters
-
string
$format The date() compatible format string.
-
string
$time The formatted date string to interpret.
Returns
static
Throws
InvalidArgumentException
createNative() ¶ protected
createNative(Cake\Chronos\ChronosDateDateTimeInterface|string $time, DateTimeZone|string|null $timezone): DateTimeImmutable
Initializes the PHP DateTimeImmutable object.
Parameters
-
Cake\Chronos\ChronosDateDateTimeInterface|string
$time Fixed or relative time
-
DateTimeZone|string|null
$timezone The time zone used for 'now'
Returns
DateTimeImmutable
day() ¶ public
day(int $value): static
Set the instance's day
Parameters
-
int
$value The day value.
Returns
static
diff() ¶ public
diff(Cake\Chronos\ChronosDate $target, bool $absolute = false): DateInterval
Returns the difference between this instance and target.
Parameters
-
Cake\Chronos\ChronosDate
$target Target instance
-
bool
$absolute optional Whether the interval is forced to be positive
Returns
DateInterval
diffFiltered() ¶ public
diffFiltered(DateInterval $interval, callable $callback, Cake\Chronos\ChronosDate|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.
-
Cake\Chronos\ChronosDate|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(Cake\Chronos\ChronosDate|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: 5 months ago
When comparing a value in the future to default now: 5 months from now
When comparing a value in the past to another value: 5 months before
When comparing a value in the future to another value: 5 months after
Parameters
-
Cake\Chronos\ChronosDate|null
$other optional The datetime to compare with.
-
bool
$absolute optional removes 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(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true): int
Get the difference in days
Parameters
-
Cake\Chronos\ChronosDate|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
diffInDaysFiltered() ¶ public
diffInDaysFiltered(callable $callback, Cake\Chronos\ChronosDate|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.
-
Cake\Chronos\ChronosDate|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
diffInMonths() ¶ public
diffInMonths(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true): int
Get the difference in months
Parameters
-
Cake\Chronos\ChronosDate|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
diffInWeekdays() ¶ public
diffInWeekdays(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true, int $options = 0): int
Get the difference in weekdays
Parameters
-
Cake\Chronos\ChronosDate|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(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true, int $options = 0): int
Get the difference in weekend days using a filter
Parameters
-
Cake\Chronos\ChronosDate|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(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true): int
Get the difference in weeks
Parameters
-
Cake\Chronos\ChronosDate|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
diffInYears() ¶ public
diffInYears(Cake\Chronos\ChronosDate|null $other = null, bool $absolute = true): int
Get the difference in years
Parameters
-
Cake\Chronos\ChronosDate|null
$other optional The instance to difference from.
-
bool
$absolute optional Get the absolute of the difference
Returns
int
endOfCentury() ¶ public
endOfCentury(): static
Resets the date to end of the century and time to 23:59:59
Returns
static
endOfWeek() ¶ public
endOfWeek(): static
Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
Returns
static
equals() ¶ public
equals(Cake\Chronos\ChronosDate $other): bool
Determines if the instance is equal to another
Parameters
-
Cake\Chronos\ChronosDate
$other The instance to compare with.
Returns
bool
farthest() ¶ public
farthest(Cake\Chronos\ChronosDate $first, Cake\Chronos\ChronosDate $second, Cake\Chronos\ChronosDate ...$others): self
Get the farthest date from the instance.
Parameters
-
Cake\Chronos\ChronosDate
$first The instance to compare with.
-
Cake\Chronos\ChronosDate
$second The instance to compare with.
-
Cake\Chronos\ChronosDate
...$others Others instance to compare with.
Returns
self
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
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
greaterThan() ¶ public
greaterThan(Cake\Chronos\ChronosDate $other): bool
Determines if the instance is greater (after) than another
Parameters
-
Cake\Chronos\ChronosDate
$other The instance to compare with.
Returns
bool
greaterThanOrEquals() ¶ public
greaterThanOrEquals(Cake\Chronos\ChronosDate $other): bool
Determines if the instance is greater (after) than or equal to another
Parameters
-
Cake\Chronos\ChronosDate
$other The instance to compare with.
Returns
bool
isFirstHalf() ¶ public
isFirstHalf(): bool
Determines if the instance is within the first half of year
Returns
bool
isFuture() ¶ public
isFuture(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is in the future, ie. greater (after) than now
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
Returns
bool
isLastMonth() ¶ public
isLastMonth(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is within the last month
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
Returns
bool
isLastWeek() ¶ public
isLastWeek(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is within the last week
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
Returns
bool
isLastYear() ¶ public
isLastYear(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is within the last year
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
Returns
bool
isNextMonth() ¶ public
isNextMonth(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is within the next month
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
Returns
bool
isNextWeek() ¶ public
isNextWeek(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is within the next week
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
Returns
bool
isNextYear() ¶ public
isNextYear(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is within the next year
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
Returns
bool
isPast() ¶ public
isPast(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is in the past, ie. less (before) than now
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
Returns
bool
isSecondHalf() ¶ public
isSecondHalf(): bool
Determines if the instance is within the second half of year
Returns
bool
isToday() ¶ public
isToday(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is today
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
Returns
bool
isTomorrow() ¶ public
isTomorrow(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is tomorrow
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
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
isYesterday() ¶ public
isYesterday(DateTimeZone|string|null $timezone = null): bool
Determines if the instance is yesterday
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
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(Cake\Chronos\ChronosDate $other): bool
Determines if the instance is less (before) than another
Parameters
-
Cake\Chronos\ChronosDate
$other The instance to compare with.
Returns
bool
lessThanOrEquals() ¶ public
lessThanOrEquals(Cake\Chronos\ChronosDate $other): bool
Determines if the instance is less (before) or equal to another
Parameters
-
Cake\Chronos\ChronosDate
$other The instance to compare with.
Returns
bool
modify() ¶ public
modify(string $modifier): static
Creates a new instance with date modified according to DateTimeImmutable::modifier().
Attempting to change a time component will raise an exception
Parameters
-
string
$modifier Date modifier
Returns
static
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(Cake\Chronos\ChronosDate $other): bool
Determines if the instance is not equal to another
Parameters
-
Cake\Chronos\ChronosDate
$other The instance to compare with.
Returns
bool
now() ¶ public static
now(DateTimeZone|string|null $timezone = null): static
Get today's date.
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for now.
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\ChronosDateDateTimeInterface|string $time): 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\ChronosDateDateTimeInterface|string
$time The strtotime compatible string to parse
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
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
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
setToStringFormat() ¶ public static
setToStringFormat(string $format): void
Sets the __toString() format.
Parameters
-
string
$format See
format()
for accepted specifiers.
Returns
void
startOfCentury() ¶ public
startOfCentury(): static
Resets the date to the first day of the century
Returns
static
startOfDecade() ¶ public
startOfDecade(): static
Resets the date to the first day of the decade
Returns
static
startOfMonth() ¶ public
startOfMonth(): static
Resets the date to the first day of the month
Returns
static
startOfWeek() ¶ public
startOfWeek(): static
Resets the date to the first day of week (defined in $weekStartsAt)
Returns
static
startOfYear() ¶ public
startOfYear(): static
Resets the date to the first day of the year
Returns
static
sub() ¶ public
sub(DateInterval $interval): static
Subtract an Interval from a Date.
Any changes to the time will be ignored and reset to 00:00:00
Parameters
-
DateInterval
$interval The interval to modify this date by.
Returns
static
subDays() ¶ public
subDays(int $value): static
Remove days from the instance
Parameters
-
int
$value The number of days 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
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
toDateTimeImmutable() ¶ public
toDateTimeImmutable(DateTimeZone|string|null $timezone = null): DateTimeImmutable
Returns the date as a DateTimeImmutable
instance at midnight.
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone the DateTimeImmutable instance will be in
Returns
DateTimeImmutable
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(DateTimeZone|string|null $timezone = null): DateTimeImmutable
Returns the date as a DateTimeImmutable
instance at midnight.
Alias of toDateTimeImmutable()
.
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone the DateTimeImmutable instance will be in
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
Get today's date.
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for today.
Returns
static
tomorrow() ¶ public static
tomorrow(DateTimeZone|string|null $timezone = null): static
Get tomorrow's date.
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for tomorrow.
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
Get yesterday's date.
Parameters
-
DateTimeZone|string|null
$timezone optional Time zone to use for yesterday.
Returns
static
Property Detail
$diffFormatter ¶ protected static
Instance of the diff formatting object.
Type
Cake\Chronos\DifferenceFormatterInterface|null
$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
$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>