$datetime->relativeTimeStr() method

Given a unix timestamp (or date string), returns a formatted string indicating the time relative to now

For example:

  • 2 years ago
  • 3 months ago
  • 1 day ago
  • 30 seconds ago
  • Just now
  • 1 day from now
  • 5 months from now
  • 3 years from now

This method also supports multi-language and will output in the current user's language, so long as the phrases in /wire/core/WireDateTime.php are translated in the language pack.

Usage

// basic usage
$string = $datetime->relativeTimeStr($ts);

// usage with all arguments
$string = $datetime->relativeTimeStr($ts, $abbreviate = false, bool $useTense = true);

Arguments

NameType(s)Description
tsint, string

Unix timestamp or date string

abbreviate (optional)bool, int, array

Whether to use abbreviations for shorter strings.

  • Specify boolean TRUE for abbreviations (abbreviated where common, not always different from non-abbreviated)
  • Specify integer 1 for extra short abbreviations (all terms abbreviated into shortest possible string)
  • Specify boolean FALSE or omit for no abbreviations.
  • Specify associative array of key=value pairs of terms to use for abbreviations. The possible keys are: just now, ago, from now, never, second, minute, hour, day, week, month, year, decade, seconds, minutes, hours, days, weeks, months, years, decades
useTense (optional)bool

Whether to append a tense like "ago" or "from now".

  • May be ok to disable in situations where all times are assumed in future or past.
  • In abbreviate=1 (shortest) mode, this removes the leading "+" or "-" from the string.

Return value

string

Formatted relative time string


Hooking $datetime->relativeTimeStr(…)

You can add your own hook events that are executed either before or after the $datetime->relativeTimeStr(…) method is executed. Examples of both are included below. A good place for hook code such as this is in your /site/ready.php file.

Hooking before

The 'before' hooks are called immediately before each $datetime->relativeTimeStr(…) method call is executed. This type of hook is especially useful for modifying arguments before they are sent to the method.

$this->addHookBefore('WireDateTime::relativeTimeStr', function(HookEvent $event) {
  // Get the object the event occurred on, if needed
  $datetime = $event->object;

  // Get values of arguments sent to hook (and optionally modify them)
  $ts = $event->arguments(0);
  $abbreviate = $event->arguments(1);
  $useTense = $event->arguments(2);

  /* Your code here, perhaps modifying arguments */

  // Populate back arguments (if you have modified them)
  $event->arguments(0, $ts);
  $event->arguments(1, $abbreviate);
  $event->arguments(2, $useTense);
});

Hooking after

The 'after' hooks are called immediately after each $datetime->relativeTimeStr(…) method call is executed. This type of hook is especially useful for modifying the value that was returned by the method call.

$this->addHookAfter('WireDateTime::relativeTimeStr', function(HookEvent $event) {
  // Get the object the event occurred on, if needed
  $datetime = $event->object;

  // An 'after' hook can retrieve and/or modify the return value
  $return = $event->return;

  // Get values of arguments sent to hook (if needed)
  $ts = $event->arguments(0);
  $abbreviate = $event->arguments(1);
  $useTense = $event->arguments(2);

  /* Your code here, perhaps modifying the return value */

  // Populate back return value, if you have modified it
  $event->return = $return;
});

$datetime methods and properties

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #554
    In the 554th issue of ProcessWire Weekly we'll check out the latest core updates, introduce a couple of new third party modules, and more. Read on!
    Weekly.pw / 21 December 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • Subscribe to weekly ProcessWire news

“ProcessWire is like a breath of fresh air. So powerful yet simple to build with and customise, and web editors love it too.” —Margaret Chatwin, Web developer