Core\Time\Date::at
Core\Time\Date::at(int $year, uint $month, uint $day): Core\Time\DateCore\Time\Date::at( int $year, uint $month, uint $day, ): Core\Time\Date
Creates a date from a year, a month and a day. You can also name the arguments: Date::at(year: 2024, month: 12, day: 24).
A Core\Time\Date is one day in the calendar. It has no time of day and no time zone, so it is the right type for a birthday, a due date or a holiday.
at checks the three numbers. The month must be 1 to 12, and the day must exist in that month of that year. When the numbers are not a date in the calendar, such as 30 February, at throws a RuntimeError. You can catch that error to check a date that a person typed into a form.
Good to know: the year must be between -9999 and 9999. == compares two dates by their day. Date::at(2024, 12, 24) == Date::fromIso('2024-12-24') is true.
Parameters
$yearintThe year, from-9999to9999.$monthuintThe month,1to12.$dayuintThe day of the month, which must exist in that month.
Return value
Returns Core\Time\Date. The Date.
Errors
RuntimeErrorThe year, the month or the day is outside its range. It is also thrown when the date does not exist in the calendar, such as 30 February.
Changelog
| Version | Change |
|---|---|
0.0.6 | The parameters of Date::at are named year, month and day. |
Examples
Building a date
<?nvs// Shows how to build a date from a year, a month and a day.
// `Core\Time\Date::at` takes the year, the month and the day. This prints 2024-07-01.var $start = Core\Time\Date::at(2024, 7, 1);echo $start->format('yyyy-MM-dd'), "\n";
// You can also name the arguments. This prints 2024-12-24.var $party = Core\Time\Date::at(year: 2024, month: 12, day: 24);echo $party->format('yyyy-MM-dd'), "\n";
// Two dates built from the same three numbers are the same date.// `compareTo` returns 0 for them. This prints 0.echo Core\Time\Date::at(2024, 7, 1)->compareTo($start), "\n";2024-07-012024-12-240A date that does not exist
<?nvs// Shows what `Core\Time\Date::at` does with a date that is not in the calendar.
// 2024 is a leap year, so 29 February exists. This prints 2024-02-29.echo Core\Time\Date::at(2024, 2, 29)->format('yyyy-MM-dd'), "\n";
// 2023 is not a leap year, so `at` throws a `RuntimeError`.try { Core\Time\Date::at(2023, 2, 29);} catch (RuntimeError $error) { echo 'Not a date: ', $error->message, "\n";}
// A month number must be from 1 to 12. Month 13 throws a `RuntimeError` too.try { Core\Time\Date::at(2024, 13, 1);} catch (RuntimeError $error) { echo 'Not a date: ', $error->message, "\n";}2024-02-29Not a date: Core\Time\Date::at(): parameter 'day' for `2023-02` is invalid, must be in range `1..=28`Not a date: Core\Time\Date::at(): parameter 'month' is not in the required range of 1..=12Checking a birthday from a form
<?nvs// Shows how to check the birthday a person typed into a form with three fields.
class Form{ // `at` throws a `RuntimeError` when the three numbers are not a real date. // The function returns a message for the form in both cases. public static function birthday(int $year, uint $month, uint $day): string { try { var $born = Core\Time\Date::at($year, $month, $day); return 'Saved: ' . $born->format('d MMMM yyyy'); } catch (RuntimeError $error) { return 'Please check the date: ' . $year . '-' . $month . '-' . $day; } }}
// The second date does not exist, because April has 30 days.echo Form::birthday(1990, 4, 12), "\n";echo Form::birthday(1990, 4, 31), "\n";echo Form::birthday(2000, 2, 29), "\n";Saved: 12 April 1990Please check the date: 1990-4-31Saved: 29 February 2000