TimeInterval
Helper class to manipulate and format time intervals (durations).
Supports various input formats:
- Numeric values (int|float) as seconds
- Colon-separated strings: "MM:SS" or "HH:MM:SS"
- Human-readable units: "1.5d 3h 15m 12.5s"
Useful for displaying or converting durations to readable forms,
such as "2h 5m"
or "2:05:00"
, or for computing the total seconds or minutes.
Basic usage:
$duration = new TimeInterval('7:31');
echo $duration->humanize(); // 7m 31s
echo $duration->formatted(); // 7:31
echo $duration->toSeconds(); // 451
echo $duration->toMinutes(); // 7.5166
echo $duration->toMinutes(null, 0); // 8
With hour/minute/second string:
$duration = new TimeInterval('1h 2m 5s');
echo $duration->humanize(); // 1h 2m 5s
echo $duration->formatted(); // 1:02:05
echo $duration->toSeconds(); // 3725
With days and custom hours/day:
$duration = new TimeInterval('1.5d 1.5h 2m 5s', 6);
echo $duration->humanize(); // 1d 4h 32m 5s
echo $duration->formatted(); // 10:32:05
echo $duration->toMinutes(); // 632.083
Raw seconds:
$duration = new TimeInterval(4293);
echo $duration->humanize() ; // 1h 11m 33s
echo $duration->formatted() ; // 1:11:33
Tags
Table of Contents
Properties
- $days : int|float|null
- $hours : int|float|null
- $hoursPerDay : int|null
- $minutes : int|float|null
- $seconds : int|float|null
- $daysRegex : string
- $hoursRegex : string
- $minutesRegex : string
- $secondsRegex : string
Methods
- __construct() : mixed
- Creates a new TimeInterval instance.
- formatted() : string
- Returns the duration as a colon formatted string
- humanize() : string
- Returns the duration as a human-readable string.
- parse() : self|bool
- Attempt to parse one of the forms of duration.
- reset() : void
- Resets the Duration object by clearing the output and values.
- toMinutes() : int|float
- Returns the duration as an amount of minutes.
- toSeconds() : int|float
- Returns the duration as an amount of seconds.
- numberBreakdown() : array<string|int, mixed>
Properties
$days
public
int|float|null
$days
Hooks
public
int|float|null
get
$hours
public
int|float|null
$hours
Hooks
public
int|float|null
get
$hoursPerDay
public
int|null
$hoursPerDay
Hooks
public
int|null
get
$minutes
public
int|float|null
$minutes
Hooks
public
int|float|null
get
$seconds
public
int|float|null
$seconds
Hooks
public
int|float|null
get
$daysRegex
private
string
$daysRegex
= '/(\d+(?:\.\d+)?)\s*d/i'
$hoursRegex
private
string
$hoursRegex
= '/(\d+(?:\.\d+)?)\s*h/i'
$minutesRegex
private
string
$minutesRegex
= '/(\d+)\s*m/i'
$secondsRegex
private
string
$secondsRegex
= '/(\d+(?:\.\d+)?)\s*s/i'
Methods
__construct()
Creates a new TimeInterval instance.
public
__construct([int|float|string|null $duration = null ][, int $hoursPerDay = 24 ]) : mixed
The $duration parameter can be:
- an integer or float representing the duration in seconds,
- a string formatted as "HH:MM", "HH:MM:SS", or a string containing time units (e.g. "1h 30m 15s", "2.5d 4h"),
- or null for an initial zero duration.
The $hoursPerDay parameter sets the number of hours in a day for calculations involving days (default is 24).
Parameters
- $duration : int|float|string|null = null
-
Initial duration to parse or null for zero.
- $hoursPerDay : int = 24
-
Number of hours per day (used for day-to-hour conversions).
formatted()
Returns the duration as a colon formatted string
public
formatted([int|float|string|null $duration = null ][, bool $zeroFill = false ]) : string
For example, one hour and 42 minutes would be "1:43" With $zeroFill to true :
- 42 minutes would be "0:42:00"
- 28 seconds would be "0:00:28"
Parameters
- $duration : int|float|string|null = null
-
A string or number, representing a duration
- $zeroFill : bool = false
-
A boolean, to force zero-fill result or not (see example)
Return values
stringhumanize()
Returns the duration as a human-readable string.
public
humanize([int|float|string|null $duration = null ]) : string
For example, one hour and 42 minutes would be "1h 42m"
Parameters
- $duration : int|float|string|null = null
-
A string or number, representing a duration
Return values
stringparse()
Attempt to parse one of the forms of duration.
public
parse(int|float|string|null $duration) : self|bool
Parameters
- $duration : int|float|string|null
-
A string or number, representing a duration
Return values
self|bool —returns the TimeInterval object if successful, otherwise false
reset()
Resets the Duration object by clearing the output and values.
public
reset() : void
Tags
toMinutes()
Returns the duration as an amount of minutes.
public
toMinutes([int|float|string|null $duration = null ][, int|bool $precision = false ]) : int|float
For example, one hour and 42 minutes would be "102" minutes
Parameters
- $duration : int|float|string|null = null
-
A string or number, representing a duration
- $precision : int|bool = false
-
Number of decimal digits to round to. If set to false, the number is not rounded.
Return values
int|floattoSeconds()
Returns the duration as an amount of seconds.
public
toSeconds([int|float|string|null $duration = null ][, int|bool $precision = false ]) : int|float
For example, one hour and 42 minutes would be "6120"
Parameters
- $duration : int|float|string|null = null
-
A string or number, representing a duration
- $precision : int|bool = false
-
Number of decimal digits to round to. If set to false, the number is not rounded.
Return values
int|floatnumberBreakdown()
private
numberBreakdown(float $number) : array<string|int, mixed>
Parameters
- $number : float