Skip to content

Novis\Intl\RelativeTime::format

Novis\Intl\RelativeTime::format(
    array<{ count: int, unit: Unit }> $items,
    string $locale,
    ?{ width?: Width, numeric?: Numeric } $options = null,
): array<string>

Writes an amount of time before or after now, such as "3 days ago" or "in 2 hours".

RelativeTime::format returns one string for each item, in the same order as the list. Each item has a count and a unit, such as { count: -3, unit: RelativeTime::Unit::Day }. A negative count is in the past, so that item is "3 days ago". A positive count is in the future, so { count: 2, unit: RelativeTime::Unit::Hour } is "in 2 hours". The locale decides the words. In German, the first item is "vor 3 Tagen".

The options change how the text looks. numeric: RelativeTime::Numeric::Auto uses a word where the language has one, such as "yesterday" for a count of -1 days. The default is RelativeTime::Numeric::Always, which writes "1 day ago". width: Width::Short writes "in 2 hr.", and Width::Narrow is shorter still where the language has a shorter form.

RelativeTime::format throws a LogicError when the locale tag is not valid.

Good to know: this method does not read the clock. You work out the count and the unit yourself, for example from the difference between two Core\Time values.

Parameters

  • $itemsarray<{ count: int, unit: Unit }>
  • $localestring
  • $options?{ width?: Width, numeric?: Numeric }default null

Return value

Returns array<string>.

Examples

When a comment was written

<?nvs
// Shows how to write when each comment on a page was written.
use Novis\Intl\RelativeTime;
// A negative count is in the past.
var $ages = [
{ count: -3, unit: RelativeTime::Unit::Day },
{ count: -1, unit: RelativeTime::Unit::Hour },
{ count: -20, unit: RelativeTime::Unit::Minute },
];
// This prints 3 days ago | 1 hour ago | 20 minutes ago.
echo Core\Str::join(RelativeTime::format($ages, 'en'), ' | '), "\n";
Output
3 days ago | 1 hour ago | 20 minutes ago

Yesterday and tomorrow

<?nvs
// Shows how to write words such as "yesterday" instead of "1 day ago".
use Novis\Intl\RelativeTime;
var $days = [
{ count: -1, unit: RelativeTime::Unit::Day },
{ count: 0, unit: RelativeTime::Unit::Day },
{ count: 1, unit: RelativeTime::Unit::Day },
];
// By default, every item has a number. This prints 1 day ago | in 0 days | in 1 day.
echo Core\Str::join(RelativeTime::format($days, 'en'), ' | '), "\n";
// `RelativeTime::Numeric::Auto` uses a word where English has one. This prints yesterday | today | tomorrow.
echo Core\Str::join(RelativeTime::format($days, 'en', { numeric: RelativeTime::Numeric::Auto }), ' | '), "\n";
Output
1 day ago | in 0 days | in 1 day
yesterday | today | tomorrow

Short text in german

<?nvs
// Shows the same times in German, in the long and in the short width.
use Novis\Intl\RelativeTime;
use Novis\Intl\Width;
var $times = [
{ count: 2, unit: RelativeTime::Unit::Hour },
{ count: -5, unit: RelativeTime::Unit::Minute },
];
// This prints in 2 Stunden | vor 5 Minuten.
echo Core\Str::join(RelativeTime::format($times, 'de'), ' | '), "\n";
// This prints in 2 Std. | vor 5 Min.
echo Core\Str::join(RelativeTime::format($times, 'de', { width: Width::Short }), ' | '), "\n";
Output
in 2 Stunden | vor 5 Minuten
in 2 Std. | vor 5 Min.