code
stringlengths 31
1.39M
| docstring
stringlengths 23
16.8k
| func_name
stringlengths 1
126
| language
stringclasses 1
value | repo
stringlengths 7
63
| path
stringlengths 7
166
| url
stringlengths 50
220
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
public function __isset(string $name): bool
{
try {
$this->__get($name);
} catch (InvalidArgumentException $e) {
return false;
}
return true;
} | Check if an attribute exists on the object
@param string $name The property name to check.
@return bool Whether the property exists. | __isset | php | cakephp/chronos | src/Chronos.php | https://github.com/cakephp/chronos/blob/master/src/Chronos.php | MIT |
public function __debugInfo(): array
{
/** @var \DateTimeZone $timezone */
$timezone = $this->getTimezone();
$properties = [
'hasFixedNow' => static::hasTestNow(),
'time' => $this->format('Y-m-d H:i:s.u'),
'timezone' => $timezone->getName(),
];
return $properties;
} | Return properties for debugging.
@return array | __debugInfo | php | cakephp/chronos | src/Chronos.php | https://github.com/cakephp/chronos/blob/master/src/Chronos.php | MIT |
protected function createNative(
ChronosDate|DateTimeInterface|string $time,
DateTimeZone|string|null $timezone,
): DateTimeImmutable {
if (!is_string($time)) {
return new DateTimeImmutable($time->format('Y-m-d 00:00:00'));
}
$timezone ??= date_default_timezone_get();
$timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
$testNow = Chronos::getTestNow();
if ($testNow === null) {
$time = new DateTimeImmutable($time, $timezone);
return new DateTimeImmutable($time->format('Y-m-d 00:00:00'));
}
$testNow = $testNow->setTimezone($timezone);
if ($time !== 'now') {
$testNow = $testNow->modify($time);
}
return new DateTimeImmutable($testNow->format('Y-m-d 00:00:00'));
} | Initializes the PHP DateTimeImmutable object.
@param \Cake\Chronos\ChronosDate|\DateTimeInterface|string $time Fixed or relative time
@param \DateTimeZone|string|null $timezone The time zone used for 'now'
@return \DateTimeImmutable | createNative | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public static function now(DateTimeZone|string|null $timezone = null): static
{
return new static('now', $timezone);
} | Get today's date.
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return static | now | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public static function today(DateTimeZone|string|null $timezone = null): static
{
return static::now($timezone);
} | Get today's date.
@param \DateTimeZone|string|null $timezone Time zone to use for today.
@return static | today | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public static function tomorrow(DateTimeZone|string|null $timezone = null): static
{
return new static('tomorrow', $timezone);
} | Get tomorrow's date.
@param \DateTimeZone|string|null $timezone Time zone to use for tomorrow.
@return static | tomorrow | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public static function yesterday(DateTimeZone|string|null $timezone = null): static
{
return new static('yesterday', $timezone);
} | Get yesterday's date.
@param \DateTimeZone|string|null $timezone Time zone to use for yesterday.
@return static | yesterday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public static function parse(ChronosDate|DateTimeInterface|string $time): static
{
return new static($time);
} | 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()
@param \Cake\Chronos\ChronosDate|\DateTimeInterface|string $time The strtotime compatible string to parse
@return static | parse | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public static function create(int $year, int $month, int $day): static
{
$instance = static::createFromFormat(
'Y-m-d',
sprintf('%s-%s-%s', 0, $month, $day),
);
return $instance->addYears($year);
} | Create an instance from a specific date.
@param int $year The year to create an instance with.
@param int $month The month to create an instance with.
@param int $day The day to create an instance with.
@return static | create | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public static function createFromFormat(
string $format,
string $time,
): static {
$dateTime = DateTimeImmutable::createFromFormat($format, $time);
static::$lastErrors = DateTimeImmutable::getLastErrors();
if (!$dateTime) {
$message = static::$lastErrors ? implode(PHP_EOL, static::$lastErrors['errors']) : 'Unknown error';
throw new InvalidArgumentException($message);
}
return new static($dateTime);
} | Create an instance from a specific format
@param string $format The date() compatible format string.
@param string $time The formatted date string to interpret.
@return static
@throws \InvalidArgumentException | createFromFormat | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public static function getLastErrors(): array|false
{
return static::$lastErrors;
} | Returns parse warnings and errors from the last ``createFromFormat()``
call.
Returns the same data as DateTimeImmutable::getLastErrors().
@return array|false | getLastErrors | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public static function createFromArray(array $values): static
{
$formatted = sprintf('%04d-%02d-%02d ', $values['year'], $values['month'], $values['day']);
return static::parse($formatted);
} | Creates an instance from an array of date values.
Allowed values:
- year
- month
- day
@param array<int|string> $values Array of date and time values.
@return static | createFromArray | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public static function diffFormatter(?DifferenceFormatterInterface $formatter = null): DifferenceFormatterInterface
{
if ($formatter === null) {
if (static::$diffFormatter === null) {
static::$diffFormatter = new DifferenceFormatter();
}
return static::$diffFormatter;
}
return static::$diffFormatter = $formatter;
} | Get the difference formatter instance or overwrite the current one.
@param \Cake\Chronos\DifferenceFormatterInterface|null $formatter The formatter instance when setting.
@return \Cake\Chronos\DifferenceFormatterInterface The formatter instance. | diffFormatter | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function add(DateInterval $interval): static
{
if ($interval->f > 0 || $interval->s > 0 || $interval->i > 0 || $interval->h > 0) {
throw new InvalidArgumentException('Cannot add intervals with time components');
}
$new = clone $this;
$new->native = $new->native->add($interval)->setTime(0, 0, 0);
return $new;
} | Add an Interval to a Date
Any changes to the time will be ignored and reset to 00:00:00
@param \DateInterval $interval The interval to modify this date by.
@return static A modified Date instance | add | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function sub(DateInterval $interval): static
{
if ($interval->f > 0 || $interval->s > 0 || $interval->i > 0 || $interval->h > 0) {
throw new InvalidArgumentException('Cannot subtract intervals with time components');
}
$new = clone $this;
$new->native = $new->native->sub($interval)->setTime(0, 0, 0);
return $new;
} | Subtract an Interval from a Date.
Any changes to the time will be ignored and reset to 00:00:00
@param \DateInterval $interval The interval to modify this date by.
@return static A modified Date instance | sub | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function modify(string $modifier): static
{
if (preg_match('/hour|minute|second/', $modifier)) {
throw new InvalidArgumentException('Cannot modify date objects by time values');
}
$new = clone $this;
$new->native = $new->native->modify($modifier);
if ($new->native === false) {
throw new InvalidArgumentException(sprintf('Unable to modify date using `%s`', $modifier));
}
if ($new->format('H:i:s') !== '00:00:00') {
$new->native = $new->native->setTime(0, 0, 0);
}
return $new;
} | Creates a new instance with date modified according to DateTimeImmutable::modifier().
Attempting to change a time component will raise an exception
@param string $modifier Date modifier
@return static | modify | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function setDate(int $year, int $month, int $day): static
{
$new = clone $this;
$new->native = $new->native->setDate($year, $month, $day);
return $new;
} | Sets the date.
@param int $year The year to set.
@param int $month The month to set.
@param int $day The day to set.
@return static | setDate | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function setISODate(int $year, int $week, int $dayOfWeek = 1): static
{
$new = clone $this;
$new->native = $new->native->setISODate($year, $week, $dayOfWeek);
return $new;
} | Sets the date according to the ISO 8601 standard
@param int $year Year of the date.
@param int $week Week of the date.
@param int $dayOfWeek Offset from the first day of the week.
@return static | setISODate | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function diff(ChronosDate $target, bool $absolute = false): DateInterval
{
return $this->native->diff($target->native, $absolute);
} | Returns the difference between this instance and target.
@param \Cake\Chronos\ChronosDate $target Target instance
@param bool $absolute Whether the interval is forced to be positive
@return \DateInterval | diff | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function format(string $format): string
{
return $this->native->format($format);
} | Returns formatted date string according to DateTimeImmutable::format().
@param string $format String format
@return string | format | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function year(int $value): static
{
return $this->setDate($value, $this->month, $this->day);
} | Set the instance's year
@param int $value The year value.
@return static | year | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function month(int $value): static
{
return $this->setDate($this->year, $value, $this->day);
} | Set the instance's month
@param int $value The month value.
@return static | month | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function day(int $value): static
{
return $this->setDate($this->year, $this->month, $value);
} | Set the instance's day
@param int $value The day value.
@return static | day | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function addYears(int $value): static
{
$month = $this->month;
$date = $this->modify($value . ' years');
if ($date->month !== $month) {
return $date->modify('last day of previous month');
}
return $date;
} | 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
```
@param int $value The number of years to add.
@return static | addYears | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function subYears(int $value): static
{
return $this->addYears(-$value);
} | Remove years from the instance.
Has the same behavior as `addYears()`.
@param int $value The number of years to remove.
@return static | subYears | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function addYearsWithOverflow(int $value): static
{
return $this->modify($value . ' year');
} | 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
```
@param int $value The number of years to add.
@return static | addYearsWithOverflow | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function subYearsWithOverflow(int $value): static
{
return $this->addYearsWithOverflow(-1 * $value);
} | Remove years with overflow from the instance
Has the same behavior as `addYeasrWithOverflow()`.
@param int $value The number of years to remove.
@return static | subYearsWithOverflow | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function addMonths(int $value): static
{
$day = $this->day;
$date = $this->modify($value . ' months');
if ($date->day !== $day) {
return $date->modify('last day of previous month');
}
return $date;
} | 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
```
@param int $value The number of months to add.
@return static | addMonths | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function subMonths(int $value): static
{
return $this->addMonths(-$value);
} | Remove months from the instance
Has the same behavior as `addMonths()`.
@param int $value The number of months to remove.
@return static | subMonths | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function addMonthsWithOverflow(int $value): static
{
return $this->modify($value . ' months');
} | 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
```
@param int $value The number of months to add.
@return static | addMonthsWithOverflow | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function subMonthsWithOverflow(int $value): static
{
return $this->addMonthsWithOverflow(-1 * $value);
} | 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
```
@param int $value The number of months to remove.
@return static | subMonthsWithOverflow | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function addDays(int $value): static
{
return $this->modify("$value days");
} | Add days to the instance. Positive $value travels forward while
negative $value travels into the past.
@param int $value The number of days to add.
@return static | addDays | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function subDays(int $value): static
{
return $this->addDays(-$value);
} | Remove days from the instance
@param int $value The number of days to remove.
@return static | subDays | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function addWeekdays(int $value): static
{
return $this->modify($value . ' weekdays, ' . $this->format('H:i:s'));
} | Add weekdays to the instance. Positive $value travels forward while
negative $value travels into the past.
@param int $value The number of weekdays to add.
@return static | addWeekdays | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function subWeekdays(int $value): static
{
return $this->addWeekdays(-$value);
} | Remove weekdays from the instance
@param int $value The number of weekdays to remove.
@return static | subWeekdays | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function addWeeks(int $value): static
{
return $this->modify("$value week");
} | Add weeks to the instance. Positive $value travels forward while
negative $value travels into the past.
@param int $value The number of weeks to add.
@return static | addWeeks | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function subWeeks(int $value): static
{
return $this->addWeeks(-$value);
} | Remove weeks to the instance
@param int $value The number of weeks to remove.
@return static | subWeeks | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function startOfMonth(): static
{
return $this->modify('first day of this month');
} | Resets the date to the first day of the month
@return static | startOfMonth | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function endOfMonth(): static
{
return $this->modify('last day of this month');
} | Resets the date to end of the month
@return static | endOfMonth | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function startOfYear(): static
{
return $this->modify('first day of january');
} | Resets the date to the first day of the year
@return static | startOfYear | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function endOfYear(): static
{
return $this->modify('last day of december');
} | Resets the date to end of the year
@return static | endOfYear | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function startOfDecade(): static
{
$year = $this->year - $this->year % Chronos::YEARS_PER_DECADE;
return $this->modify("first day of january $year");
} | Resets the date to the first day of the decade
@return static | startOfDecade | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function endOfDecade(): static
{
$year = $this->year - $this->year % Chronos::YEARS_PER_DECADE + Chronos::YEARS_PER_DECADE - 1;
return $this->modify("last day of december $year");
} | Resets the date to end of the decade
@return static | endOfDecade | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function startOfCentury(): static
{
$year = $this->startOfYear()
->year($this->year - 1 - ($this->year - 1) % Chronos::YEARS_PER_CENTURY + 1)
->year;
return $this->modify("first day of january $year");
} | Resets the date to the first day of the century
@return static | startOfCentury | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function endOfCentury(): static
{
$y = $this->year - 1
- ($this->year - 1)
% Chronos::YEARS_PER_CENTURY
+ Chronos::YEARS_PER_CENTURY;
$year = $this->endOfYear()
->year($y)
->year;
return $this->modify("last day of december $year");
} | Resets the date to end of the century and time to 23:59:59
@return static | endOfCentury | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function startOfWeek(): static
{
$dateTime = $this;
if ($dateTime->dayOfWeek !== Chronos::getWeekStartsAt()) {
$dateTime = $dateTime->previous(Chronos::getWeekStartsAt());
}
return $dateTime;
} | Resets the date to the first day of week (defined in $weekStartsAt)
@return static | startOfWeek | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function endOfWeek(): static
{
$dateTime = $this;
if ($dateTime->dayOfWeek !== Chronos::getWeekEndsAt()) {
$dateTime = $dateTime->next(Chronos::getWeekEndsAt());
}
return $dateTime;
} | Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
@return static | endOfWeek | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function next(?int $dayOfWeek = null): static
{
if ($dayOfWeek === null) {
$dayOfWeek = $this->dayOfWeek;
}
$day = static::$days[$dayOfWeek];
return $this->modify("next $day");
} | 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.
@param int|null $dayOfWeek The day of the week to move to.
@return static | next | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function previous(?int $dayOfWeek = null): static
{
if ($dayOfWeek === null) {
$dayOfWeek = $this->dayOfWeek;
}
$day = static::$days[$dayOfWeek];
return $this->modify("last $day");
} | 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.
@param int|null $dayOfWeek The day of the week to move to.
@return static | previous | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function firstOfMonth(?int $dayOfWeek = null): static
{
$day = $dayOfWeek === null ? 'day' : static::$days[$dayOfWeek];
return $this->modify("first $day of this month");
} | 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.
@param int|null $dayOfWeek The day of the week to move to.
@return static | firstOfMonth | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function lastOfMonth(?int $dayOfWeek = null): static
{
$day = $dayOfWeek === null ? 'day' : static::$days[$dayOfWeek];
return $this->modify("last $day of this month");
} | 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.
@param int|null $dayOfWeek The day of the week to move to.
@return static | lastOfMonth | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function nthOfMonth(int $nth, int $dayOfWeek): static|false
{
$dateTime = $this->firstOfMonth();
$check = $dateTime->format('Y-m');
$dateTime = $dateTime->modify("+$nth " . static::$days[$dayOfWeek]);
return $dateTime->format('Y-m') === $check ? $dateTime : 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.
@param int $nth The offset to use.
@param int $dayOfWeek The day of the week to move to.
@return static|false | nthOfMonth | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function firstOfQuarter(?int $dayOfWeek = null): static
{
return $this
->day(1)
->month($this->quarter * Chronos::MONTHS_PER_QUARTER - 2)
->firstOfMonth($dayOfWeek);
} | 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.
@param int|null $dayOfWeek The day of the week to move to.
@return static | firstOfQuarter | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function lastOfQuarter(?int $dayOfWeek = null): static
{
return $this
->day(1)
->month($this->quarter * Chronos::MONTHS_PER_QUARTER)
->lastOfMonth($dayOfWeek);
} | 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.
@param int|null $dayOfWeek The day of the week to move to.
@return static | lastOfQuarter | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function nthOfQuarter(int $nth, int $dayOfWeek): static|false
{
$dateTime = $this->day(1)->month($this->quarter * Chronos::MONTHS_PER_QUARTER);
$lastMonth = $dateTime->month;
$year = $dateTime->year;
$dateTime = $dateTime->firstOfQuarter()->modify("+$nth" . static::$days[$dayOfWeek]);
return $lastMonth < $dateTime->month || $year !== $dateTime->year ? false : $dateTime;
} | 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.
@param int $nth The offset to use.
@param int $dayOfWeek The day of the week to move to.
@return static|false | nthOfQuarter | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function firstOfYear(?int $dayOfWeek = null): static
{
$day = $dayOfWeek === null ? 'day' : static::$days[$dayOfWeek];
return $this->modify("first $day of january");
} | 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.
@param int|null $dayOfWeek The day of the week to move to.
@return static | firstOfYear | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function lastOfYear(?int $dayOfWeek = null): static
{
$day = $dayOfWeek === null ? 'day' : static::$days[$dayOfWeek];
return $this->modify("last $day of december");
} | 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.
@param int|null $dayOfWeek The day of the week to move to.
@return static | lastOfYear | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function nthOfYear(int $nth, int $dayOfWeek): static|false
{
$dateTime = $this->firstOfYear()->modify("+$nth " . static::$days[$dayOfWeek]);
return $this->year === $dateTime->year ? $dateTime : 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.
@param int $nth The offset to use.
@param int $dayOfWeek The day of the week to move to.
@return static|false | nthOfYear | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function equals(ChronosDate $other): bool
{
return $this->native == $other->native;
} | Determines if the instance is equal to another
@param \Cake\Chronos\ChronosDate $other The instance to compare with.
@return bool | equals | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function notEquals(ChronosDate $other): bool
{
return !$this->equals($other);
} | Determines if the instance is not equal to another
@param \Cake\Chronos\ChronosDate $other The instance to compare with.
@return bool | notEquals | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function greaterThan(ChronosDate $other): bool
{
return $this->native > $other->native;
} | Determines if the instance is greater (after) than another
@param \Cake\Chronos\ChronosDate $other The instance to compare with.
@return bool | greaterThan | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function greaterThanOrEquals(ChronosDate $other): bool
{
return $this->native >= $other->native;
} | Determines if the instance is greater (after) than or equal to another
@param \Cake\Chronos\ChronosDate $other The instance to compare with.
@return bool | greaterThanOrEquals | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function lessThan(ChronosDate $other): bool
{
return $this->native < $other->native;
} | Determines if the instance is less (before) than another
@param \Cake\Chronos\ChronosDate $other The instance to compare with.
@return bool | lessThan | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function lessThanOrEquals(ChronosDate $other): bool
{
return $this->native <= $other->native;
} | Determines if the instance is less (before) or equal to another
@param \Cake\Chronos\ChronosDate $other The instance to compare with.
@return bool | lessThanOrEquals | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function between(ChronosDate $start, ChronosDate $end, bool $equals = true): bool
{
if ($start->greaterThan($end)) {
[$start, $end] = [$end, $start];
}
if ($equals) {
return $this->greaterThanOrEquals($start) && $this->lessThanOrEquals($end);
}
return $this->greaterThan($start) && $this->lessThan($end);
} | Determines if the instance is between two others
@param \Cake\Chronos\ChronosDate $start Start of target range
@param \Cake\Chronos\ChronosDate $end End of target range
@param bool $equals Whether to include the beginning and end of range
@return bool | between | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function closest(ChronosDate $first, ChronosDate $second, ChronosDate ...$others): ChronosDate
{
$closest = $first;
$closestDiffInDays = $this->diffInDays($first);
foreach ([$second, ...$others] as $other) {
$otherDiffInDays = $this->diffInDays($other);
if ($otherDiffInDays < $closestDiffInDays) {
$closest = $other;
$closestDiffInDays = $otherDiffInDays;
}
}
return $closest;
} | Get the closest date from the instance.
@param \Cake\Chronos\ChronosDate $first The instance to compare with.
@param \Cake\Chronos\ChronosDate $second The instance to compare with.
@param \Cake\Chronos\ChronosDate ...$others Others instance to compare with.
@return self | closest | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function farthest(ChronosDate $first, ChronosDate $second, ChronosDate ...$others): ChronosDate
{
$farthest = $first;
$farthestDiffInDays = $this->diffInDays($first);
foreach ([$second, ...$others] as $other) {
$otherDiffInDays = $this->diffInDays($other);
if ($otherDiffInDays > $farthestDiffInDays) {
$farthest = $other;
$farthestDiffInDays = $otherDiffInDays;
}
}
return $farthest;
} | Get the farthest date from the instance.
@param \Cake\Chronos\ChronosDate $first The instance to compare with.
@param \Cake\Chronos\ChronosDate $second The instance to compare with.
@param \Cake\Chronos\ChronosDate ...$others Others instance to compare with.
@return self | farthest | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isWeekday(): bool
{
return !$this->isWeekend();
} | Determines if the instance is a weekday
@return bool | isWeekday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isWeekend(): bool
{
return in_array($this->dayOfWeek, Chronos::getWeekendDays(), true);
} | Determines if the instance is a weekend day
@return bool | isWeekend | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isYesterday(DateTimeZone|string|null $timezone = null): bool
{
return $this->equals(static::yesterday($timezone));
} | Determines if the instance is yesterday
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isYesterday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isToday(DateTimeZone|string|null $timezone = null): bool
{
return $this->equals(static::now($timezone));
} | Determines if the instance is today
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isToday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isTomorrow(DateTimeZone|string|null $timezone = null): bool
{
return $this->equals(static::tomorrow($timezone));
} | Determines if the instance is tomorrow
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isTomorrow | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isNextWeek(DateTimeZone|string|null $timezone = null): bool
{
return $this->format('W o') === static::now($timezone)->addWeeks(1)->format('W o');
} | Determines if the instance is within the next week
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isNextWeek | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isLastWeek(DateTimeZone|string|null $timezone = null): bool
{
return $this->format('W o') === static::now($timezone)->subWeeks(1)->format('W o');
} | Determines if the instance is within the last week
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isLastWeek | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isNextMonth(DateTimeZone|string|null $timezone = null): bool
{
return $this->format('m Y') === static::now($timezone)->addMonths(1)->format('m Y');
} | Determines if the instance is within the next month
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isNextMonth | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isLastMonth(DateTimeZone|string|null $timezone = null): bool
{
return $this->format('m Y') === static::now($timezone)->subMonths(1)->format('m Y');
} | Determines if the instance is within the last month
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isLastMonth | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isNextYear(DateTimeZone|string|null $timezone = null): bool
{
return $this->year === static::now($timezone)->addYears(1)->year;
} | Determines if the instance is within the next year
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isNextYear | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isLastYear(DateTimeZone|string|null $timezone = null): bool
{
return $this->year === static::now($timezone)->subYears(1)->year;
} | Determines if the instance is within the last year
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isLastYear | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isFirstHalf(): bool
{
return $this->half === 1;
} | Determines if the instance is within the first half of year
@return bool | isFirstHalf | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isSecondHalf(): bool
{
return $this->half === 2;
} | Determines if the instance is within the second half of year
@return bool | isSecondHalf | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isFuture(DateTimeZone|string|null $timezone = null): bool
{
return $this->greaterThan(static::now($timezone));
} | Determines if the instance is in the future, ie. greater (after) than now
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isFuture | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isPast(DateTimeZone|string|null $timezone = null): bool
{
return $this->lessThan(static::now($timezone));
} | Determines if the instance is in the past, ie. less (before) than now
@param \DateTimeZone|string|null $timezone Time zone to use for now.
@return bool | isPast | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isLeapYear(): bool
{
return $this->format('L') === '1';
} | Determines if the instance is a leap year
@return bool | isLeapYear | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isSunday(): bool
{
return $this->dayOfWeek === Chronos::SUNDAY;
} | Checks if this day is a Sunday.
@return bool | isSunday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isMonday(): bool
{
return $this->dayOfWeek === Chronos::MONDAY;
} | Checks if this day is a Monday.
@return bool | isMonday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isTuesday(): bool
{
return $this->dayOfWeek === Chronos::TUESDAY;
} | Checks if this day is a Tuesday.
@return bool | isTuesday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isWednesday(): bool
{
return $this->dayOfWeek === Chronos::WEDNESDAY;
} | Checks if this day is a Wednesday.
@return bool | isWednesday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isThursday(): bool
{
return $this->dayOfWeek === Chronos::THURSDAY;
} | Checks if this day is a Thursday.
@return bool | isThursday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isFriday(): bool
{
return $this->dayOfWeek === Chronos::FRIDAY;
} | Checks if this day is a Friday.
@return bool | isFriday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isSaturday(): bool
{
return $this->dayOfWeek === Chronos::SATURDAY;
} | Checks if this day is a Saturday.
@return bool | isSaturday | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function wasWithinLast(string|int $timeInterval): bool
{
$now = new static(new Chronos());
$interval = $now->modify('-' . $timeInterval);
$thisTime = $this->format('U');
return $thisTime >= $interval->format('U') && $thisTime <= $now->format('U');
} | Returns true this instance happened within the specified interval
@param string|int $timeInterval the numeric value with space then time type.
Example of valid types: 6 hours, 2 days, 1 minute.
@return bool | wasWithinLast | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function isWithinNext(string|int $timeInterval): bool
{
$now = new static(new Chronos());
$interval = $now->modify('+' . $timeInterval);
$thisTime = $this->format('U');
return $thisTime <= $interval->format('U') && $thisTime >= $now->format('U');
} | Returns true this instance will happen within the specified interval
@param string|int $timeInterval the numeric value with space then time type.
Example of valid types: 6 hours, 2 days, 1 minute.
@return bool | isWithinNext | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function diffFiltered(
DateInterval $interval,
callable $callback,
?ChronosDate $other = null,
bool $absolute = true,
int $options = 0,
): int {
$start = $this;
$end = $other ?? new ChronosDate(Chronos::now());
$inverse = false;
if ($end < $start) {
$start = $end;
$end = $this;
$inverse = true;
}
// Hack around PHP's DatePeriod not counting equal dates at midnight as
// within the range. Sadly INCLUDE_END_DATE doesn't land until 8.2
$endTime = $end->native->modify('+1 second');
$period = new DatePeriod($start->native, $interval, $endTime, $options);
$vals = array_filter(iterator_to_array($period), function (DateTimeInterface $date) use ($callback) {
return $callback(static::parse($date));
});
$diff = count($vals);
return $inverse && !$absolute ? -$diff : $diff;
} | Get the difference by the given interval using a filter callable
@param \DateInterval $interval An interval to traverse by
@param callable $callback The callback to use for filtering.
@param \Cake\Chronos\ChronosDate|null $other The instance to difference from.
@param bool $absolute Get the absolute of the difference
@param int $options DatePeriod options, {@see https://www.php.net/manual/en/class.dateperiod.php}
@return int | diffFiltered | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function diffInYears(?ChronosDate $other = null, bool $absolute = true): int
{
$diff = $this->diff($other ?? new static(new Chronos()), $absolute);
return $diff->invert ? -$diff->y : $diff->y;
} | Get the difference in years
@param \Cake\Chronos\ChronosDate|null $other The instance to difference from.
@param bool $absolute Get the absolute of the difference
@return int | diffInYears | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function diffInMonths(?ChronosDate $other = null, bool $absolute = true): int
{
$diff = $this->diff($other ?? new static(Chronos::now()), $absolute);
$months = $diff->y * Chronos::MONTHS_PER_YEAR + $diff->m;
return $diff->invert ? -$months : $months;
} | Get the difference in months
@param \Cake\Chronos\ChronosDate|null $other The instance to difference from.
@param bool $absolute Get the absolute of the difference
@return int | diffInMonths | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function diffInWeeks(?ChronosDate $other = null, bool $absolute = true): int
{
return (int)($this->diffInDays($other, $absolute) / Chronos::DAYS_PER_WEEK);
} | Get the difference in weeks
@param \Cake\Chronos\ChronosDate|null $other The instance to difference from.
@param bool $absolute Get the absolute of the difference
@return int | diffInWeeks | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function diffInDays(?ChronosDate $other = null, bool $absolute = true): int
{
$diff = $this->diff($other ?? new static(Chronos::now()), $absolute);
return $diff->invert ? -(int)$diff->days : (int)$diff->days;
} | Get the difference in days
@param \Cake\Chronos\ChronosDate|null $other The instance to difference from.
@param bool $absolute Get the absolute of the difference
@return int | diffInDays | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function diffInDaysFiltered(
callable $callback,
?ChronosDate $other = null,
bool $absolute = true,
int $options = 0,
): int {
return $this->diffFiltered(new DateInterval('P1D'), $callback, $other, $absolute, $options);
} | Get the difference in days using a filter callable
@param callable $callback The callback to use for filtering.
@param \Cake\Chronos\ChronosDate|null $other The instance to difference from.
@param bool $absolute Get the absolute of the difference
@param int $options DatePeriod options, {@see https://www.php.net/manual/en/class.dateperiod.php}
@return int | diffInDaysFiltered | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function diffInWeekdays(?ChronosDate $other = null, bool $absolute = true, int $options = 0): int
{
return $this->diffInDaysFiltered(function (ChronosDate $date) {
return $date->isWeekday();
}, $other, $absolute, $options);
} | Get the difference in weekdays
@param \Cake\Chronos\ChronosDate|null $other The instance to difference from.
@param bool $absolute Get the absolute of the difference
@param int $options DatePeriod options, {@see https://www.php.net/manual/en/class.dateperiod.php}
@return int | diffInWeekdays | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
public function diffInWeekendDays(?ChronosDate $other = null, bool $absolute = true, int $options = 0): int
{
return $this->diffInDaysFiltered(function (ChronosDate $date) {
return $date->isWeekend();
}, $other, $absolute, $options);
} | Get the difference in weekend days using a filter
@param \Cake\Chronos\ChronosDate|null $other The instance to difference from.
@param bool $absolute Get the absolute of the difference
@param int $options DatePeriod options, {@see https://www.php.net/manual/en/class.dateperiod.php}
@return int | diffInWeekendDays | php | cakephp/chronos | src/ChronosDate.php | https://github.com/cakephp/chronos/blob/master/src/ChronosDate.php | MIT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.