Oihana PHP

durationParts.php

Table of Contents

Functions

durationParts()  : array{d: int, h: int, m: int, s: int|float}
Breaks a duration down into days, hours, minutes and seconds.

Functions

durationParts()

Breaks a duration down into days, hours, minutes and seconds.

durationParts([int|float|string|null $duration = null ][, int $hoursPerDay = 24 ]) : array{d: int, h: int, m: int, s: int|float}

The $duration may be anything durationToSeconds() reads:

  • 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 (a zero duration).

The duration is first normalized into a number of seconds, so every component stays within its natural range — seconds and minutes below 60, hours below $hoursPerDay — whatever the input form: "90:00" gives 1 hour and 30 minutes. Only the seconds may carry a fractional part (5.5); a whole number of seconds comes back as an int.

The parts are keyed by the DurationUnit suffixes, so the key a caller reads is the suffix humanizeDuration() writes. With $hoursPerDay at 0 or below, no day is counted and every hour stays in the hours.

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

The duration to break down.

$hoursPerDay : int = 24

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

Tags
example
use oihana\core\date\DurationUnit ;
use function oihana\core\date\durationParts ;

durationParts( 3725 ) ;     // [ 'd' => 0 , 'h' => 1 , 'm' => 2 , 's' => 5 ]
durationParts( '90:00' ) ;  // [ 'd' => 0 , 'h' => 1 , 'm' => 30 , 's' => 0 ]
durationParts( 5.5 ) ;      // [ 'd' => 0 , 'h' => 0 , 'm' => 0 , 's' => 5.5 ]

durationParts( 93784 )[ DurationUnit::DAY ] ; // 1
author

Marc Alcaraz (ekameleon)

since
1.3.0
Return values
array{d: int, h: int, m: int, s: int|float} —

The days, hours, minutes and seconds, keyed by DurationUnit::DAY, HOUR, MINUTE and SECOND.

On this page

Search results