wppaste
WordPress

human_readable_duration( string $duration = '' ): string|false

Since
5.1.0
Source
wp-includes/functions.php:508
Converts a duration to human readable format.

Compatibility

WordPress
since 5.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$durationstringoptional
Duration will be in string format (HH:ii:ss) OR (ii:ss), with a possible prepended negative sign (-).Default: ''

Return value

string|false
A human readable duration string, false on failure.

Performance profile

How much work a call to human_readable_duration() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
4–77

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 89.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below human_readable_duration()

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost human_readable_duration() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always4–6none
!empty($duration) && is_string($duration)24–41explode(), array_reverse()
!empty($duration) && is_string($duration) && $duration50–53explode(), array_reverse(), _n(), sprintf()
!empty($duration) && is_string($duration) && $duration62–65explode(), array_reverse(), _n(), sprintf(), _n(), sprintf()
!empty($duration) && is_string($duration) && $duration && $hour && $minute && $second74–77explode(), array_reverse(), _n(), sprintf(), _n(), sprintf(), _n(), sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev894–7710
8.5894–7710
8.4894–771023 fewer instructions than PHP 8.3
8.31124–9710
8.21124–97105 fewer instructions than PHP 8.1
8.11174–9910
7.41174–9910

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

  • str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
  • _n()Translates and retrieves the singular or plural form based on the supplied number.

Used by · 2

Source code

function human_readable_duration( $duration = '' ) {	if ( ( empty( $duration ) || ! is_string( $duration ) ) ) {		return false;	} 	$duration = trim( $duration ); 	// Remove prepended negative sign.	if ( str_starts_with( $duration, '-' ) ) {		$duration = substr( $duration, 1 );	} 	// Extract duration parts.	$duration_parts = array_reverse( explode( ':', $duration ) );	$duration_count = count( $duration_parts ); 	$hour   = null;	$minute = null;	$second = null; 	if ( 3 === $duration_count ) {		// Validate HH:ii:ss duration format.		if ( ! ( (bool) preg_match( '/^([0-9]+):([0-5]?[0-9]):([0-5]?[0-9])$/', $duration ) ) ) {			return false;		}		// Three parts: hours, minutes & seconds.		list( $second, $minute, $hour ) = $duration_parts;	} elseif ( 2 === $duration_count ) {		// Validate ii:ss duration format.		if ( ! ( (bool) preg_match( '/^([0-5]?[0-9]):([0-5]?[0-9])$/', $duration ) ) ) {			return false;		}		// Two parts: minutes & seconds.		list( $second, $minute ) = $duration_parts;	} else {		return false;	} 	$human_readable_duration = array(); 	// Add the hour part to the string.	if ( is_numeric( $hour ) ) {		/* translators: %s: Time duration in hour or hours. */		$human_readable_duration[] = sprintf( _n( '%s hour', '%s hours', $hour ), (int) $hour );	} 	// Add the minute part to the string.	if ( is_numeric( $minute ) ) {		/* translators: %s: Time duration in minute or minutes. */		$human_readable_duration[] = sprintf( _n( '%s minute', '%s minutes', $minute ), (int) $minute );	} 	// Add the second part to the string.	if ( is_numeric( $second ) ) {		/* translators: %s: Time duration in second or seconds. */		$human_readable_duration[] = sprintf( _n( '%s second', '%s seconds', $second ), (int) $second );	} 	return implode( ', ', $human_readable_duration );}

Changelog

Introduced in 5.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.