Title: human_readable_duration
Published: February 22, 2019
Last modified: February 24, 2026

---

# human_readable_duration( string $duration ): string|false

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#wp--skip-link--target)

Converts a duration to human readable format.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#parameters)󠁿

 `$duration`stringrequired

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

## 󠀁[Return](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#source)󠁿

    ```php
    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 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/functions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/functions.php#L512)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/functions.php#L512-L571)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#related)󠁿

| Uses | Description | 
| [_n()](https://developer.wordpress.org/reference/functions/_n/)`wp-includes/l10n.php` |

Translates and retrieves the singular or plural form based on the supplied number.

  |

| Used by | Description | 
| [attachment_submitbox_metadata()](https://developer.wordpress.org/reference/functions/attachment_submitbox_metadata/)`wp-admin/includes/media.php` |

Displays non-editable attachment metadata in the publish meta box.

  | 
| [wp_prepare_attachment_for_js()](https://developer.wordpress.org/reference/functions/wp_prepare_attachment_for_js/)`wp-includes/media.php` |

Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.1.0](https://developer.wordpress.org/reference/since/5.1.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#comment-content-3179)
 2.    [rabmalin](https://profiles.wordpress.org/rabmalin/)  [  7 years ago  ](https://developer.wordpress.org/reference/functions/human_readable_duration/#comment-3179)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhuman_readable_duration%2F%23comment-3179)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhuman_readable_duration%2F%23comment-3179)
 4.      ```php
         // Get duration in human readable format.
         $duration = human_readable_duration( '4:30:25' );
         echo $duration;
         ```
     
 5.  **Output:**
      `4 hours, 30 minutes, 25 seconds`
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhuman_readable_duration%2F%3Freplytocom%3D3179%23feedback-editor-3179)
 7.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/human_readable_duration/?output_format=md#comment-content-4263)
 8.    [mattiasf](https://profiles.wordpress.org/mattiasf/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/human_readable_duration/#comment-4263)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhuman_readable_duration%2F%23comment-4263)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhuman_readable_duration%2F%23comment-4263)
 10.     ```php
         // Get duration in human readable format from seconds
         $seconds = '121';
         $duration = human_readable_duration( gmdate( 'H:i:s', $seconds ) );
         echo $duration;
         ```
     
 11. **Output:**
      `0 hours, 2 minutes, 1 second`
 12.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhuman_readable_duration%2F%3Freplytocom%3D4263%23feedback-editor-4263)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhuman_readable_duration%2F)
before being able to contribute a note or feedback.