Skip to content

Novis\Intl\DateFormat::times

Writes times of day the way a language and region write them, such as "9:30 PM" or "21:30".

DateFormat::times takes a list of Core\Time\TimeOfDay values and a locale tag, and returns one string for each time, in the same order. The locale decides between a 12-hour and a 24-hour clock. In American English, half past nine in the evening gives "9:30 PM". In German, it gives "21:30".

The result shows hours and minutes. Set the seconds option to true to show the seconds too, as in "9:30:15 PM".

DateFormat::times throws a LogicError when the locale tag is not valid.

Good to know: a Core\Time\TimeOfDay has no date and no time zone. To write the time of a Core\Time\DateTime, call $value->timeOfDay() first.

Parameters

  • $valuesarray<Core\Time\TimeOfDay>
  • $localestring
  • $options?{ seconds?: bool }default null

Return value

Returns array<string>.

Examples

A 12 hour and a 24 hour clock

<?nvs
// Shows that the locale decides between a 12-hour and a 24-hour clock.
use Novis\Intl\DateFormat;
var $closes = Core\Time\TimeOfDay::at(21, 30);
// This prints 9:30 PM.
echo DateFormat::times([$closes], 'en-US')[0], "\n";
// This prints 21:30.
echo DateFormat::times([$closes], 'de')[0], "\n";
Output
9:30 PM
21:30

Show the seconds

<?nvs
// Shows how to add the seconds to a time of day.
use Novis\Intl\DateFormat;
var $finish = Core\Time\TimeOfDay::at(21, 30, { second: 15 });
// Without options, the seconds are not shown. This prints 9:30 PM.
echo DateFormat::times([$finish], 'en-US')[0], "\n";
// `seconds: true` shows them. This prints 9:30:15 PM.
echo DateFormat::times([$finish], 'en-US', { seconds: true })[0], "\n";
Output
9:30 PM
9:30:15 PM

Opening hours

<?nvs
// Shows how to write a shop's opening hours in the reader's language.
use Novis\Intl\DateFormat;
array<Core\Time\TimeOfDay> $hours = [Core\Time\TimeOfDay::at(9, 0), Core\Time\TimeOfDay::at(18, 30)];
// One call writes both times. This prints 9:00 AM – 6:30 PM.
array<string> $english = DateFormat::times($hours, 'en-US');
echo $english[0], ' – ', $english[1], "\n";
// This prints 09:00 – 18:30.
array<string> $french = DateFormat::times($hours, 'fr');
echo $french[0], ' – ', $french[1], "\n";
Output
9:00 AM – 6:30 PM
09:00 – 18:30