Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Helper.php

function Helper::formatTime

2 calls to Helper::formatTime()
ProgressBar::initPlaceholderFormatters in vendor/symfony/console/Helper/ProgressBar.php
ProgressIndicator::initPlaceholderFormatters in vendor/symfony/console/Helper/ProgressIndicator.php

File

vendor/symfony/console/Helper/Helper.php, line 88

Class

Helper
Helper is the base class for all helper classes.

Namespace

Symfony\Component\Console\Helper

Code

public static function formatTime(int|float $secs, int $precision = 1) : string {
    $secs = (int) floor($secs);
    if (0 === $secs) {
        return '< 1 sec';
    }
    static $timeFormats = [
        [
            1,
            '1 sec',
            'secs',
        ],
        [
            60,
            '1 min',
            'mins',
        ],
        [
            3600,
            '1 hr',
            'hrs',
        ],
        [
            86400,
            '1 day',
            'days',
        ],
    ];
    $times = [];
    foreach ($timeFormats as $index => $format) {
        $seconds = isset($timeFormats[$index + 1]) ? $secs % $timeFormats[$index + 1][0] : $secs;
        if (isset($times[$index - $precision])) {
            unset($times[$index - $precision]);
        }
        if (0 === $seconds) {
            continue;
        }
        $unitCount = $seconds / $format[0];
        $times[$index] = 1 === $unitCount ? $format[1] : $unitCount . ' ' . $format[2];
        if ($secs === $seconds) {
            break;
        }
        $secs -= $seconds;
    }
    return implode(', ', array_reverse($times));
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal