Novis\Intl\DateFormat::dates
Novis\Intl\DateFormat::dates(
array<Core\Time\Date> $values,
string $locale,
?{ length?: Length, fields?: Fields } $options = null,
): array<string>Writes calendar dates the way a language and region write them, such as "Dec 24, 2026" or "24.12.2026".
DateFormat::dates takes a list of Core\Time\Date values and a locale tag, and returns one string for each date, in the same order. The locale decides the order of day, month and year, the month names and the separators. A locale tag is a language and region, such as en-GB or ja. With en-GB, December 24, 2026 gives "24 Dec 2026". With ja, the same date gives "2026/12/24".
The length option is DateFormat::Length::Short, DateFormat::Length::Medium or DateFormat::Length::Long, and the default is DateFormat::Length::Medium. In German, March 1, 2026 gives "01.03.26" with Short, "01.03.2026" with Medium and "1. März 2026" with Long.
The fields option chooses which parts of the date are written. It is a DateFormat::Fields, and the default is DateFormat::Fields::YearMonthDay. With DateFormat::Length::Long in English, DateFormat::Fields::YearMonth gives "December 2026". DateFormat::Fields::MonthDayWeekday gives "Thursday, December 24". The locale decides the order of the parts.
DateFormat::dates throws a LogicError when the locale tag is not valid.
Good to know: a Core\Time\Date has no time of day and no time zone. To write the date of a Core\Time\DateTime, call $value->date() first.
Parameters
$valuesarray<Core\Time\Date>$localestring$options?{ length?: Length, fields?: Fields }default null
Return value
Returns array<string>.
Changelog
| Version | Change |
|---|---|
0.0.6 | Novis\Intl\DateFormat::dates and Novis\Intl\DateFormat::dateTimes take a fields option, a case of DateFormat::Fields. On dateTimes, the cases without a day throw a LogicError. Use dates to show only a month or a year. |
Examples
A date in five languages
<?nvs// Shows how one date is written in five languages.use Novis\Intl\DateFormat;
var $holiday = Core\Time\Date::at(2026, 12, 24);array<string> $locales = ['en-US', 'en-GB', 'de', 'ja', 'fr'];
// The locale decides the order of day, month and year.// This prints Dec 24, 2026, then 24 Dec 2026, 24.12.2026, 2026/12/24 and 24 déc. 2026.foreach ($locales as string $locale) { echo DateFormat::dates([$holiday], $locale)[0], "\n";}Dec 24, 202624 Dec 202624.12.20262026/12/2424 déc. 2026Short medium and long
<?nvs// Shows the three lengths of a date.use Novis\Intl\DateFormat;
var $start = Core\Time\Date::at(2026, 3, 1);array<DateFormat::Length> $lengths = [DateFormat::Length::Short, DateFormat::Length::Medium, DateFormat::Length::Long];
// This prints 01.03.26, then 01.03.2026, then 1. März 2026.foreach ($lengths as DateFormat::Length $length) { echo DateFormat::dates([$start], 'de', { length: $length })[0], "\n";}01.03.2601.03.20261. März 2026A list of delivery dates
<?nvs// Shows how to write a whole list of dates with one call.use Novis\Intl\DateFormat;
// The order dates come from a `Core\Time\DateTime` each. `date()` keeps only the calendar date.var $zone = Core\Time\Zone::of('Europe/Berlin');array<Core\Time\Date> $deliveries = [ Core\Time\DateTime::fromIso('2026-05-04 10:00', $zone)->date(), Core\Time\DateTime::fromIso('2026-05-11 16:00', $zone)->date(),];
// The result has one string for each date, in the same order.// This prints 4. Mai 2026, then 11. Mai 2026.foreach (DateFormat::dates($deliveries, 'de', { length: DateFormat::Length::Long }) as string $line) { echo $line, "\n";}4. Mai 202611. Mai 2026Choose the parts of a date
<?nvs// Shows how the `fields` option chooses which parts of a date are written.use Novis\Intl\DateFormat;
array<Core\Time\Date> $dates = [Core\Time\Date::at(2026, 12, 24)];
// `YearMonth` shows the month and the year. This prints December 2026.echo DateFormat::dates($dates, 'en-US', { fields: DateFormat::Fields::YearMonth, length: DateFormat::Length::Long })[0], "\n";
// `MonthDayWeekday` adds the day of the week. This prints Thursday, December 24.echo DateFormat::dates($dates, 'en-US', { fields: DateFormat::Fields::MonthDayWeekday, length: DateFormat::Length::Long })[0], "\n";
// The locale decides the order of the parts. This prints Donnerstag, 24. Dezember.echo DateFormat::dates($dates, 'de', { fields: DateFormat::Fields::MonthDayWeekday, length: DateFormat::Length::Long })[0], "\n";December 2026Thursday, December 24Donnerstag, 24. Dezember