Novis\Intl\DateFormat::dateTimes
Novis\Intl\DateFormat::dateTimes(
array<Core\Time\DateTime> $values,
string $locale,
?{ length?: Length, fields?: Fields, seconds?: bool, zone?: ZoneStyle } $options = null,
): array<string>Writes dates with their time of day the way a language and region write them, such as "Mar 1, 2026, 9:30 PM" or "01.03.2026, 21:30".
DateFormat::dateTimes takes a list of Core\Time\DateTime values and a locale tag, and returns one string for each value. Each Core\Time\DateTime has its own time zone, and the time is written as a clock in that zone shows it.
length is a DateFormat::Length, and the default is Medium. In English, Short gives "3/1/26, 9:30 PM" and Long gives "March 1, 2026 at 9:30 PM". Set seconds to true to show the seconds.
zone is a DateFormat::ZoneStyle and adds the time zone. For a time in Vienna, Offset adds the difference from UTC, "GMT+1". Location adds the country, "Austria Time". Generic adds the name of the zone, "Central European Time". The default is None, which adds nothing.
fields is a DateFormat::Fields and chooses the parts of the date. The default is YearMonthDay.
DateFormat::dateTimes throws a LogicError when the locale tag is not valid. It also throws one when fields has no day, such as YearMonth.
Good to know: the method does not change a value's time zone. Convert the value to the reader's zone first.
Parameters
$valuesarray<Core\Time\DateTime>$localestring$options?{ length?: Length, fields?: Fields, seconds?: bool, zone?: ZoneStyle }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
An order time in each language
<?nvs// Shows how to write the time of an order in the reader's language.use Novis\Intl\DateFormat;
var $order = Core\Time\DateTime::fromIso('2026-03-01 21:30', 'Europe/Vienna');
// American English uses a 12-hour clock. This prints Mar 1, 2026, 9:30 PM.echo DateFormat::dateTimes([$order], 'en-US')[0], "\n";
// German and Japanese use a 24-hour clock. This prints 01.03.2026, 21:30 and 2026/03/01 21:30.echo DateFormat::dateTimes([$order], 'de')[0], "\n";echo DateFormat::dateTimes([$order], 'ja')[0], "\n";Mar 1, 2026, 9:30 PM01.03.2026, 21:302026/03/01 21:30Show the time zone
<?nvs// Shows the three ways to add the time zone to a date and time.use Novis\Intl\DateFormat;
var $meeting = Core\Time\DateTime::fromIso('2026-03-01 21:30', 'Europe/Vienna');
// `DateFormat::ZoneStyle::Offset` adds the difference to UTC. This prints Mar 1, 2026, 9:30 PM GMT+1.echo DateFormat::dateTimes([$meeting], 'en-US', { zone: DateFormat::ZoneStyle::Offset })[0], "\n";
// `DateFormat::ZoneStyle::Location` adds the place. This prints Mar 1, 2026, 9:30 PM Austria Time.echo DateFormat::dateTimes([$meeting], 'en-US', { zone: DateFormat::ZoneStyle::Location })[0], "\n";
// `DateFormat::ZoneStyle::Generic` adds the zone's name. This prints Mar 1, 2026, 9:30 PM Central European Time.echo DateFormat::dateTimes([$meeting], 'en-US', { zone: DateFormat::ZoneStyle::Generic })[0], "\n";Mar 1, 2026, 9:30 PM GMT+1Mar 1, 2026, 9:30 PM Austria TimeMar 1, 2026, 9:30 PM Central European TimeOne moment in two zones
<?nvs// Shows that each value is written with the clock of its own time zone.use Novis\Intl\DateFormat;
var $vienna = Core\Time\DateTime::fromIso('2026-03-01 21:30', 'Europe/Vienna');var $newYork = $vienna->toInstant()->in('America/New_York');
// Both values are the same moment. One call writes both, and each shows its own clock.// This prints March 1, 2026 at 9:30 PM, then March 1, 2026 at 3:30 PM.foreach (DateFormat::dateTimes([$vienna, $newYork], 'en-US', { length: DateFormat::Length::Long }) as string $line) { echo $line, "\n";}March 1, 2026 at 9:30 PMMarch 1, 2026 at 3:30 PMA weekday with the time
<?nvs// Shows how the `fields` option chooses the parts of the date that appear before the time.use Novis\Intl\DateFormat;
var $meeting = Core\Time\DateTime::fromIso('2026-03-02 09:30', 'Europe/Vienna');
// `Weekday` shows only the day of the week. This prints Montag, 9:30.// German writes the time in a different way when only the weekday is shown.echo DateFormat::dateTimes([$meeting], 'de', { fields: DateFormat::Fields::Weekday, length: DateFormat::Length::Long })[0], "\n";
// `MonthDayWeekday` also shows the day and the month. This prints Montag, 2. März um 09:30.echo DateFormat::dateTimes([$meeting], 'de', { fields: DateFormat::Fields::MonthDayWeekday, length: DateFormat::Length::Long })[0], "\n";
// `YearMonth` has no day, so it cannot be shown with a time. This throws a `LogicError`.try { DateFormat::dateTimes([$meeting], 'de', { fields: DateFormat::Fields::YearMonth });} catch (LogicError $error) { echo $error->message, "\n";}Montag, 9:30Montag, 2. März um 09:30`Fields::YearMonth` shows no day, so it cannot be shown with a time. Format the date with `DateFormat::dates` instead.