The human_readable_duration() WordPress PHP function converts a duration to human-readable format.
Usage
$readable_duration = human_readable_duration('4:30:25');
Input: ‘4:30:25’
Output: ‘4 hours, 30 minutes, 25 seconds’
Parameters
- $duration (string, optional): Duration in the format (HH:ii:ss) or (ii:ss), with a possible prepended negative sign (-). Default: ”
More information
See WordPress Developer Resources: human_readable_duration
Examples
Basic usage
Convert a duration to a human-readable format.
$duration = '4:30:25'; $readable_duration = human_readable_duration($duration); echo $readable_duration; // Output: 4 hours, 30 minutes, 25 seconds
Negative duration
Convert a negative duration to a human-readable format.
$duration = '-2:15:10'; $readable_duration = human_readable_duration($duration); echo $readable_duration; // Output: -2 hours, 15 minutes, 10 seconds
Duration in minutes and seconds
Convert a duration in minutes and seconds to a human-readable format.
$duration = '45:15'; $readable_duration = human_readable_duration($duration); echo $readable_duration; // Output: 45 minutes, 15 seconds
Duration from seconds
Convert a duration in seconds to a human-readable format.
$seconds = 121; $duration = gmdate('H:i:s', $seconds); $readable_duration = human_readable_duration($duration); echo $readable_duration; // Output: 0 hours, 2 minutes, 1 second
Empty duration
Handle an empty duration string.
$duration = ''; $readable_duration = human_readable_duration($duration); echo $readable_duration; // Output: 0 hours,