Oihana PHP

humanizeDuration.php

Table of Contents

Functions

humanizeDuration()  : string
Returns a human-readable representation of a duration, e.g. `"1d 2h 3m 4s"`.

Functions

humanizeDuration()

Returns a human-readable representation of a duration, e.g. `"1d 2h 3m 4s"`.

humanizeDuration([int|float|string|null $duration = null ][, int $hoursPerDay = 24 ]) : string

The $duration may be:

  • an int|float number of seconds (e.g. 3725),
  • a colon string "MM:SS" or "HH:MM:SS",
  • a unit string "1.5d 3h 15m 12.5s" (any subset, any order),
  • or null (treated as a zero duration → "0s").

Whatever the input form, the duration is first normalized into a number of seconds and then broken down so that every component stays within its natural range (seconds < 60, minutes < 60, hours < $hoursPerDay). Fractional input therefore rolls up correctly: "90:00" and "1.5h" both yield "1h 30m". Only the seconds component may carry a fractional part (e.g. "5.5s").

Parameters
$duration : int|float|string|null = null

The duration to format.

$hoursPerDay : int = 24

Hours in a day for day↔hour conversion (default 24).

Tags
example
use function oihana\core\date\humanizeDuration ;

echo humanizeDuration( 3725 ) ;        // "1h 2m 5s"
echo humanizeDuration( '1h 30m' ) ;    // "1h 30m"
echo humanizeDuration( '90:00' ) ;     // "1h 30m"
echo humanizeDuration( null ) ;        // "0s"
author

Marc Alcaraz (ekameleon)

since
1.2.0
Return values
string

The human-readable duration (e.g. "1h 42m"), "0s" for an empty duration.

On this page

Search results