$datetime->elapsedTimeStr() method

Render an elapsed time string

If the $stop argument is omitted then it is assumed to be the current time. The maximum period used is weeks, as months and years are not fixed length periods.

Available since version 3.0.129.

Example

$start = '2023-09-08 08:33:52';
$stop = '2023-09-09 10:47:23';

// Regular: 1 day 2 hours 13 minutes 31 seconds
echo $datetime->elapsedTimeStr($start, $stop);

// Abbreviated: 1 day 2 hrs 13 mins 31 secs
echo $datetime->elapsedTimeStr($start, $stop, true);

// Abbreviated with exclusions: 1 day 2 hrs
echo $datetime->elapsedTimeStr($start, $stop, true, [ 'exclude' => 'minutes seconds' ]);

// Optional 3.0.227+ usage and inclusions: 26 hours 13 minutes
echo $datetime->elapsedTimeStr($start, [ 'stop' => $stop, 'include' => 'hours minutes' ]);

Usage

// basic usage
$string = $datetime->elapsedTimeStr($start);

// usage with all arguments
$string = $datetime->elapsedTimeStr($start, $stop = null, $abbreviate = false, array $options = []);

Arguments

NameType(s)Description
startint, string

Starting timestamp or date/time string.

stop (optional)int, string, array

Ending timestamp or date/time string, omit for “now”, or:

  • In 3.0.227+ you may optionally substitute the $options array argument here. When doing so, all remaining arguments are ignored and the stop and abbreviate (if needed) may be specified in the given options array, along with any other options.
abbreviate (optional)bool, int, array
  • Specify boolean FALSE for verbose elapsed time string without abbreviations (default).
  • 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 integer 0 for digital elapsed time string like “00:01:12” referring to hours:minutes:seconds.
  • Note that when using 0 no options apply except except for exclude[seconds] option.
options (optional)array

Additional options:

  • delimiter (string): String to separate time periods (default=' ').
  • getArray (bool): Return an array of integers indexed by period name (rather than a string)? 3.0.227+
  • exclude (array|string): Exclude these periods, one or more of: 'seconds', 'minutes', 'hours', 'days', 'weeks' (default=[])
  • include (array|string): Include only these periods, one or more of: 'seconds', 'minutes', 'hours', 'days', 'weeks' (default=[]) 3.0.227+
  • Note the exclude and include options should not be used together, and the include option requires 3.0.227+.

Return value

string array

Returns array only if the getArray option is true, otherwise returns a string.


$datetime methods and properties

API reference based on ProcessWire core version 3.0.234

Latest news

  • ProcessWire Weekly #510
    In the 510th issue of ProcessWire Weekly we'll check out ProcessWire 3.0.236, introduce a new third party module PageFinder Depth from Robin Sallis, and more. Read on!
    Weekly.pw / 17 February 2024
  • Using date range fields in ProcessWire
    This week we'll take a detailed look at the newest addition to the ProFields set of modules: the Date Range Fieldtype and Inputfield.
    Blog / 24 November 2023
  • Subscribe to weekly ProcessWire news

“We chose ProcessWire because of its excellent architecture, modular extensibility and the internal API. The CMS offers the necessary flexibility and performance for such a complex website like superbude.de. ProcessWire offers options that are only available for larger systems, such as Drupal, and allows a much slimmer development process.” —xport communication GmbH