Skip to content

Core\Time\DateTime::parse

Reads a date and time from text, using a pattern that says how the text is written, and returns a DateTime in the time zone you give.

The pattern uses letters for each part: yyyy is the year, MM the month, dd the day, HH the hour and mm the minute. Other characters, such as - or ., must appear in the text exactly as written. A pattern without a time gives midnight. A text that does not match the pattern throws a ParseError, and the message says where the text stopped matching. So does a time outside about the years -9999 to 9999. A pattern that is not valid, or that names a time zone, throws a LogicError.

The examples below show a date in a European format, texts that do not match, and times from a file converted to UTC.

Parameters

  • $textstringThe text to read. The result does not keep a qualifier of the text, such as tainted.
  • $formatstringA CLDR pattern with date and time letters only, such as yyyy-MM-dd HH:mm. $zone gives the zone. A pattern is a small language, so a tainted pattern is not allowed.
  • $zoneCore\Time\ZoneThe zone the date and time are read in. If the zone skips the time, the result moves forward past the gap.

Return value

Returns Core\Time\DateTime. The DateTime in $zone.

Errors

  • LogicError$format is not valid. It has a pattern letter that is not supported or a quote that is not closed. It is also thrown when $format has a zone or offset letter, because $zone already gives the zone. For a $format written directly in the code, only an invalid pattern is error E0769. A zone or offset letter is a valid pattern that only this method does not allow. So it throws this error however $format was written.
  • ParseError$text does not match $format. Fixed text differs, a number has no digits, there is extra text at the end, or the parts together are not a real date and time. It is also thrown for a time outside -9999-01-02T01:59:59Z..=9999-12-30T22:00:00Z.

Changelog

VersionChange
0.0.5Core\Time\DateTime::fromIso reads a local date and time, such as 2024-03-01 09:30, in the zone you give. Core\Time\DateTime::at builds a DateTime from a Date, a TimeOfDay and a zone. Core\Time\DateTime::parse reads text in a pattern you choose.

Examples

A date in a european format

<?nvs
// Shows how to read a date and time that is written in a local format.
// The pattern `dd.MM.yyyy HH:mm` matches the text "05.06.2024 14:30".
// The time is read in Berlin, so the offset is +02:00.
var $berlin = Core\Time\Zone::of('Europe/Berlin');
var $meeting = Core\Time\DateTime::parse('05.06.2024 14:30', 'dd.MM.yyyy HH:mm', $berlin);
echo $meeting->format('yyyy-MM-dd HH:mm XXX'), "\n";
// This pattern has no time, so the result is midnight on that day.
var $day = Core\Time\DateTime::parse('05.06.2024', 'dd.MM.yyyy', $berlin);
echo $day->format('yyyy-MM-dd HH:mm XXX'), "\n";
Output
2024-06-05 14:30 +02:00
2024-06-05 00:00 +02:00

Texts that do not match

<?nvs
// Shows what happens when a text does not match the pattern.
// Only the first text matches. The others throw a `ParseError`. The message says what is
// wrong: a `T` instead of a space, a one-digit month, or a day that does not exist.
var $utc = Core\Time\Zone::of('UTC');
array<string> $inputs = ['2024-06-05 14:30', '2024-06-05T14:30', '2024-6-5 14:30', '2024-02-30 14:30'];
foreach ($inputs as string $text) {
try {
echo Core\Time\DateTime::parse($text, 'yyyy-MM-dd HH:mm', $utc)->format('yyyy-MM-dd HH:mm'), "\n";
} catch (ParseError $error) {
echo 'not valid: ', $text, "\n";
echo ' ', $error->message, "\n";
}
}
Output
2024-06-05 14:30
not valid: 2024-06-05T14:30
Core\Time\DateTime::parse(): expected ` ` at offset 10
not valid: 2024-6-5 14:30
Core\Time\DateTime::parse(): expected 2 or more digits of month at offset 6
not valid: 2024-02-30 14:30
Core\Time\DateTime::parse(): parameter 'day' for `2024-02` is invalid, must be in range `1..=29`

Times from a file stored as utc

<?nvs
// Shows how to read times from an exported file and convert them to UTC before you store them.
// The file writes times as "06/05/2024 02:30 PM", in New York time.
// `hh` is the hour on a 12-hour clock, and `a` reads "AM" or "PM".
array<string> $rows = ['06/05/2024 02:30 PM', '12/31/2024 11:59 PM', '01/01/2025 09:05 AM'];
var $newYork = Core\Time\Zone::of('America/New_York');
// `toInstant` gives the moment in time, and `toIso` writes it in UTC.
foreach ($rows as string $row) {
var $local = Core\Time\DateTime::parse($row, 'MM/dd/yyyy hh:mm a', $newYork);
echo $row, ' -> ', $local->toInstant()->toIso(), "\n";
}
Output
06/05/2024 02:30 PM -> 2024-06-05T18:30:00Z
12/31/2024 11:59 PM -> 2025-01-01T04:59:00Z
01/01/2025 09:05 AM -> 2025-01-01T14:05:00Z