Oihana PHP

durationToSeconds.php

Table of Contents

Functions

durationToSeconds()  : float
Normalizes a duration expressed in any supported form into a number of seconds.

Functions

durationToSeconds()

Normalizes a duration expressed in any supported form into a number of seconds.

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

The $duration may be:

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

A day is worth $hoursPerDay hours, so the d unit of a unit string is converted accordingly (a colon string never carries a day component).

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

The duration to convert.

$hoursPerDay : int = 24

Hours in a day for the d unit (default 24).

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

durationToSeconds( 3725 ) ;        // 3725.0
durationToSeconds( '90:00' ) ;     // 5400.0
durationToSeconds( '1h 30m' ) ;    // 5400.0
durationToSeconds( '1.5d' , 8 ) ;  // 43200.0  (1.5 × 8h)
durationToSeconds( null ) ;        // 0.0
author

Marc Alcaraz (ekameleon)

since
1.2.0
Return values
float

The total number of seconds (0.0 for an empty duration).

On this page

Search results