Oihana PHP System

TimeInterval

The TimeInterval helper to format a duration.

Tags
example
$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
echo $duration->toMinutes(null, 2); // 7.52
$duration = new Duration('7:31');
$duration = new Duration('1h 2m 5s');

echo $duration->humanize();  // 1h 2m 5s
echo $duration->formatted(); // 1:02:05
echo $duration->toSeconds(); // 3725
echo $duration->toMinutes(); // 62.0833
echo $duration->toMinutes(null, 0); // 62
// Configured for 6 hours per day
$duration = new Duration('1.5d 1.5h 2m 5s', 6);

echo $duration->humanize();  // 1d 4h 32m 5s
echo $duration->formatted(); // 10:32:05
echo $duration->toSeconds(); // 37925
echo $duration->toMinutes(); // 632.083333333
echo $duration->toMinutes(null, 0); // 632
$duration = new Duration('4293');

echo $duration->humanize();  // 1h 11m 33s
echo $duration->formatted(); // 1:11:33
echo $duration->toSeconds(); // 4293
echo $duration->toMinutes(); // 71.55
echo $duration->toMinutes(null, 0); // 72

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
$output  : string|int
$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.
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>
output()  : string
Returns the output of the Duration object and resets.
reset()  : void
Resets the Duration object by clearing the output and values.

Properties

Methods

__construct()

Creates a new TimeInterval instance.

public __construct([int|float|string|null $duration = null ][, int $hoursPerDay = 24 ]) : mixed
Parameters
$duration : int|float|string|null = null
$hoursPerDay : int = 24

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
string

humanize()

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
string

parse()

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 Duration object if successful, otherwise false

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|float

toSeconds()

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|float

numberBreakdown()

private numberBreakdown(float $number) : array<string|int, mixed>
Parameters
$number : float
Return values
array<string|int, mixed>

output()

Returns the output of the Duration object and resets.

private output() : string
Tags
access

private

Return values
string

reset()

Resets the Duration object by clearing the output and values.

private reset() : void
Tags
access

private


        
On this page

Search results