$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 #553
    In the 553rd issue of ProcessWire Weekly we'll check out the latest weekly update from Ryan, introduce a new third party module called Text Synthesis, and more. Read on!
    Weekly.pw / 14 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

“We were really happy to build our new portfolio website on ProcessWire! We wanted something that gave us plenty of control on the back-end, without any bloat on the front end - just a nice, easy to access API for all our content that left us free to design and build however we liked.” —Castus, web design agency in Sheffield, UK