Core\Time\Date::fromIso
Core\Time\Date::fromIso(string $text): Core\Time\DateCore\Time\Date::fromIso( string $text, ): Core\Time\Date
Reads a date written as text, such as 2024-03-01, and returns a Core\Time\Date. The text follows ISO 8601, the international standard way to write a date.
The text must have the form YYYY-MM-DD. Other ISO 8601 forms are not allowed, such as 20240301, the week date 2024-W09-5 or the day of the year 2024-061.
Text in another form throws a ParseError. So does a date that does not exist, such as 2024-02-30. The message says which part is wrong. Text written directly in the code is checked at compile time, so Date::fromIso('2024-02-30') does not compile.
The same rules read a Date in a route path, a #[Core\Query] value, a command argument and JSON that Core\Json::decodeAs reads.
Good to know: the year must be between -9999 and 9999. A year before year 0 starts with -, such as -0044. Year 0 is written 0000, and -0000 throws a ParseError.
The examples below read a date, show texts that are not valid dates, and check whether an invoice is overdue.
Parameters
$textstringThe date asYYYY-MM-DD. A year before year 0 starts with-. Year 0 is written0000.
Return value
Returns Core\Time\Date. The Date.
Errors
ParseError$textis not in this form, such as2024-3-1, or it is not a real date, such as2024-02-30. The year-0000also throws. The error says which part is wrong.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Time\Date::fromIso reads a date such as 2024-03-01. Core\Time\TimeOfDay::fromIso reads a time such as 09:30 or 09:30:15.5. A string literal in either method is checked when the program compiles. |
0.0.6 | Date::fromIso('-0000-01-01') throws a ParseError. Year 0 is written 0000. |
Examples
Reading a date
<?nvs// Shows how to read a date that is written as text in the form `YYYY-MM-DD`.
// `fromIso` returns a `Core\Time\Date`. This prints 2024-03-01.var $start = Core\Time\Date::fromIso('2024-03-01');echo $start->format('yyyy-MM-dd'), "\n";
// The date has its parts as numbers. 2024 is a leap year, so 1 March is day 61.echo $start->year(), ' ', $start->month(), ' ', $start->dayOfYear(), "\n";
// The same date built with `at` is equal, so `compareTo` returns 0.echo $start->compareTo(Core\Time\Date::at(2024, 3, 1)), "\n";2024-03-012024 3 610Texts that are not dates
<?nvs// Shows what happens when a text is not a valid date in the form `YYYY-MM-DD`.
// The second text has no `-` between the parts. The third has a day that does not exist.// Both throw a `ParseError`, and its message says which part is wrong.array<string> $inputs = ['2024-03-01', '20240301', '2024-02-30'];foreach ($inputs as string $text) { try { echo Core\Time\Date::fromIso($text)->format('yyyy-MM-dd'), "\n"; } catch (ParseError $error) { echo $error->message, "\n"; }}2024-03-01Core\Time\Date::fromIso(): expected `-` at byte 4, in the form `YYYY-MM-DD`Core\Time\Date::fromIso(): 2024-02 has no day 30Is an invoice overdue
<?nvs// Shows how to check whether an invoice is overdue, from the date a web service returned.
class Invoice{ // An invoice must be paid within 30 days of the day it was sent. // `compareTo` returns 1 when the first date is later than the second. public static function isOverdue(string $sent, Core\Time\Date $today): bool { var $due = Core\Time\Date::fromIso($sent)->plus(30, Core\Unit::Day); return $today->compareTo($due) == 1; }}
// The first invoice was due on 2024-06-30. The second is due on 2024-07-20.var $today = Core\Time\Date::at(2024, 7, 10);echo Invoice::isOverdue('2024-05-31', $today) ? 'overdue' : 'not due yet', "\n";echo Invoice::isOverdue('2024-06-20', $today) ? 'overdue' : 'not due yet', "\n";overduenot due yet