$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.236

Latest news

  • ProcessWire Weekly #518
    The 518th issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 13 April 2024
  • ProFields Table Field with Actions support
    This week we have some updates for the ProFields table field (FieldtypeTable). These updates are primarily focused on adding new tools for the editor to facilitate input and management of content in a table field.
    Blog / 12 April 2024
  • Subscribe to weekly ProcessWire news

“I am currently managing a ProcessWire site with 2 million+ pages. It’s admirably fast, and much, much faster than any other CMS we tested.” —Nickie, Web developer