Skip to content

Core\Time\Date::parse

Reads a date from text. You give a pattern that says how the text is written, such as dd.MM.yyyy. Date::parse('05.01.2026', 'dd.MM.yyyy') returns the date 2026-01-05. The pattern uses CLDR letters, the standard pattern letters that format also uses. dd is the day, MM is the month and yyyy is the year.

The pattern may contain date letters only. A time letter such as HH is not allowed. A zone letter such as XXX, for the time zone, is not allowed either. A Date has no time and no zone. When the pattern is a string literal, the program does not compile. When the pattern is a variable, parse throws a LogicError.

When the text does not match the pattern, parse throws a ParseError. This happens when a number is missing, when there is extra text at the end, or when the date does not exist, such as 30.02.2026.

When the pattern has no day, the day is 1. When it has no month, the month is 1. Date::parse('03/2026', 'MM/yyyy') returns 2026-03-01.

Good to know: Date::fromIso reads the format yyyy-MM-dd with no pattern. format writes a date with the same pattern letters.

Parameters

  • $textstringThe text to read. The result is a Date, so it is not tainted, even when the text is. A tainted value comes from outside the program and is not checked yet.
  • $formatstringA pattern with date letters only, such as d MMMM yyyy. A time letter such as HH or a zone letter such as XXX is not allowed, because a Date has no time and no zone. A tainted pattern is not allowed either.

Return value

Returns Core\Time\Date. The Date. Date::parse('05.01.2026', 'dd.MM.yyyy') is 2026-01-05.

Errors

  • LogicError$format is not a valid pattern, or it has a time or zone letter. When $format is a string literal, this is error E0769, and the program does not compile.
  • ParseError$text does not match $format. For example, a character such as . is different, a number is missing, or there is extra text at the end. It is also thrown when the date does not exist, such as 30.02.2026.

Changelog

VersionChange
0.0.6Core\Time\Date::parse reads a date with a pattern, such as dd.MM.yyyy. Core\Time\Date::range returns every date from one date to another, both included. Its step option can be a day, week, month, quarter or year.

Examples

A day month year date

<?nvs
// Shows how to read a date that is written with the day first.
use Core\Time\Date;
// `dd.MM.yyyy` is the day, the month and the year with dots. This prints 2026-01-05.
echo Date::parse('05.01.2026', 'dd.MM.yyyy')->toIso(), "\n";
// `MMMM` is the full name of the month. This prints 2026-03-03.
echo Date::parse('3 March 2026', 'd MMMM yyyy')->toIso(), "\n";
Output
2026-01-05
2026-03-03

Text that does not match

<?nvs
// Shows what `parse` does with text that does not match the pattern.
use Core\Time\Date;
// February 2026 has 28 days, so this date does not exist. `parse` throws a `ParseError`.
try {
Date::parse('30.02.2026', 'dd.MM.yyyy');
} catch (ParseError $missing) {
echo "30.02.2026 is not a date\n";
}
// The text has an extra letter at the end. `parse` throws a `ParseError` here too.
try {
Date::parse('05.01.2026x', 'dd.MM.yyyy');
} catch (ParseError $extra) {
echo "05.01.2026x does not match the pattern\n";
}
// The pattern `MM/yyyy` has a month and a year, but no day. So the day is 1.
// This prints 2026-03-01.
echo Date::parse('03/2026', 'MM/yyyy')->toIso(), "\n";
Output
30.02.2026 is not a date
05.01.2026x does not match the pattern
2026-03-01

Dates from a form

<?nvs
// Shows how to check the birth dates that people typed into a form.
use Core\Time\Date;
array<string> $typed = ['24.12.1990', '7.4.1985', '31.04.2001', 'next week'];
foreach ($typed as string $text) {
// `d.M.yyyy` also reads a day or month with one digit, such as `7.4.1985`.
// In `format`, `EEEE` is the full name of the weekday.
try {
var $born = Date::parse($text, 'd.M.yyyy');
echo $text, ' is ', $born->format('EEEE d MMMM yyyy'), "\n";
} catch (ParseError $wrong) {
echo $text, " is not a valid date. Write it as day.month.year.\n";
}
}
Output
24.12.1990 is Monday 24 December 1990
7.4.1985 is Sunday 7 April 1985
31.04.2001 is not a valid date. Write it as day.month.year.
next week is not a valid date. Write it as day.month.year.