PHP code example of elephant-php / duration

1. Go to this page and download the library: Download elephant-php/duration library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elephant-php / duration example snippets


use ElephantPhp\Duration\Duration;

$start    = new DateTimeImmutable('2026-01-01 10:00:00');
$end      = new DateTimeImmutable('2026-01-01 11:01:01');

$duration = Duration::between($start, $end);

$duration = Duration::fromSeconds(3661);

$duration = Duration::since($startedAt);

$duration->hours();
$duration->minutes();
$duration->seconds();

$duration->toTotalSeconds();
$duration->toTotalMinutes();
$duration->toTotalHours();

// arguments are labels
$duration->format('h', 'm', 's'); // "1 h, 1 m, 1 s"

$duration->toHms();              // "01:01:01"

$duration = Duration::fromSeconds(3661);

echo $duration->format('h', 'm', 's'); // 1 h, 1 m, 1 s
echo $duration->toHms();               // 01:01:01

echo $duration->seconds();             // 1
echo $duration->minutes();             // 1
echo $duration->hours();               // 1

echo $duration->toTotalSeconds();      // 3661
echo $duration->toTotalMinutes();      // 61
echo $duration->toTotalHours();        // 1
bash
composer