Core\Time\Date
A date with no time of day and no zone, such as a birthday or a due date. Core\Time\Date::at builds one from a year, a month and a day. plus and minus move it by days, months or years, and format turns it into text.
A Date is a year, a month and a day. It has no time of day and no zone. Date::fromIso reads a date written as YYYY-MM-DD, such as 2024-03-01. Date::parse reads a date that is written another way, with a pattern such as dd.MM.yyyy. Date::at builds one from three numbers, and ->date() gives the date of a DateTime. A date that does not exist throws an error when it is built. A date written as a string literal in Date::fromIso is checked when the program compiles.
year(), month() and day() return the three fields as numbers. weekday() returns a Core\Time\Weekday case, dayOfYear() returns the day of the year from 1, and isLeapYear() checks the year. DateTime::at($date, TimeOfDay::at(0, 0), $zone) returns the first moment of the date in a zone.
plus and minus move a date by Core\Unit::Day or a larger unit. A smaller unit throws an error. Moving by months keeps the day inside the new month, as DateTime::plus does. format takes the date letters of the patterns DateTime::format uses. A time letter or a zone letter does not compile, because a Date has no time and no zone. <, >, <=, >= and compareTo order two dates. toIso() returns the date as text such as 2024-01-31, and fromIso reads that text back.
startOf and endOf return the first and the last day of the week, month, quarter or year of a date. A week runs from Monday to Sunday. difference counts the whole days, weeks, months, quarters or years from one date to another. next and previous return the nearest date after or before a date that is on a given Core\Time\Weekday. daysInMonth() returns the number of days in the month, and quarter() returns 1 to 4. isoWeek() returns the ISO 8601 week number, and isoWeekYear() returns the year that this week belongs to. Date::range returns the list of dates from one date to another, with one date for each day, week, month, quarter or year.
<?nvsvar $d = Core\Time\Date::fromIso('2024-01-31');echo $d->format('yyyy-MM-dd'), "\n";echo $d->format('EEEE, d MMMM'), "\n";
echo $d->plus(1, Core\Unit::Month)->format('yyyy-MM-dd'), "\n";echo $d->minus(1, Core\Unit::Day)->format('EEE d MMM'), "\n";echo $d->with({ month: 3 })->format('yyyy-MM-dd'), "\n";echo $d->compareTo(Core\Time\Date::at(2024, 2, 1)), "\n";
try { Core\Time\Date::at(2023, 2, 29);} catch (RuntimeError $bad) { echo "2023-02-29 does not exist\n";}
echo Core\Time\DateTime::fromIso('2024-03-01 23:00', 'UTC')->date()->format('d/M/yyyy'), "\n";
echo $d->year(), ' ', $d->month(), ' ', $d->day(), "\n";echo $d->dayOfYear(), ' ', $d->isLeapYear() ? 'leap year' : 'not a leap year', "\n";var $midnight = Core\Time\TimeOfDay::at(0, 0);echo Core\Time\DateTime::at($d, $midnight, 'Europe/Berlin')->format('yyyy-MM-dd HH:mm XXX'), "\n";2024-01-31Wednesday, 31 January2024-02-29Tue 30 Jan2024-03-31-12023-02-29 does not exist1/3/20242024 1 3131 leap year2024-01-31 00:00 +01:00Returned by
You get a Core\Time\Date from these methods:
Members
25 members.
| Member | Signature |
|---|---|
at | at(int $year, uint $month, uint $day): Core\Time\Date |
fromIso | fromIso(string $text): Core\Time\Date |
parse | parse(string $text, string $format): Core\Time\Date |
range | range(Core\Time\Date $from, Core\Time\Date $to, { step?: Core\Unit::Day|Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year }): array<Core\Time\Date> |
format | format(string $pattern): string |
plus | plus(int $count, Core\Unit $unit): Core\Time\Date |
minus | minus(int $count, Core\Unit $unit): Core\Time\Date |
with | with({ year?: int, month?: uint, day?: uint }): Core\Time\Date |
compareTo | compareTo(Core\Time\Date $other): int |
toIso | toIso(): string |
weekday | weekday(): Core\Time\Weekday |
dayOfYear | dayOfYear(): uint |
isLeapYear | isLeapYear(): bool |
startOf | startOf(Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year $unit): Core\Time\Date |
endOf | endOf(Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year $unit): Core\Time\Date |
difference | difference(Core\Time\Date $other, Core\Unit::Day|Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year $unit): int |
next | next(Core\Time\Weekday $w): Core\Time\Date |
previous | previous(Core\Time\Weekday $w): Core\Time\Date |
daysInMonth | daysInMonth(): uint |
quarter | quarter(): uint |
isoWeek | isoWeek(): uint |
isoWeekYear | isoWeekYear(): int |
year | year(): int |
month | month(): uint |
day | day(): uint |
