lib/helpers/Date.php
public static function niceDuration($seconds, $limitPeriods = 7, $zeros = false)
    {
        // Define time periods
        $periods = [
            'years'     => 31556926,
            'months'    => 2629743,
            //'weeks'     => 604800,
            'days'      => 86400,
            'hours'     => 3600,
            'minutes'   => 60,
            'seconds'   => 1,
        ];

        // Break into periods
        $seconds = (float) $seconds;
        $segments = [];
        foreach ($periods as $period => $value) {
            if ($limitPeriods >= 1) {
                $count = floor($seconds / $value);
                $seconds = $seconds % $value;
            } else {
                $count = round($seconds / $value);
                $seconds = 0;
            }
            if (($count == 0 && !$zeros) or $limitPeriods < 1) {
                continue;
            }
            $segments[strtolower($period)] = $count;

            $limitPeriods--;
        }

        // Build the string
        $string = [];
        foreach ($segments as $key => $value) {
            $segment_name = substr($key, 0, -1);
            $segment = $value . ' ' . $segment_name;
            if ($value != 1) {
                $segment .= 's';
            }
            $string[] = $segment;
        }

        return implode(', ', $string);
    }
public static function niceDuration($seconds, $limitPeriods = 7, $zeros = false)
    {
        // Define time periods
        $periods = [
            'years'     => 31556926,
            'months'    => 2629743,
            //'weeks'     => 604800,
            'days'      => 86400,
            'hours'     => 3600,
            'minutes'   => 60,
            'seconds'   => 1,
        ];

        // Break into periods
        $seconds = (float) $seconds;
        $segments = [];
        foreach ($periods as $period => $value) {
            if ($limitPeriods >= 1) {
                $count = floor($seconds / $value);
                $seconds = $seconds % $value;
            } else {
                $count = round($seconds / $value);
                $seconds = 0;
            }
            if (($count == 0 && !$zeros) or $limitPeriods < 1) {
                continue;
            }
            $segments[strtolower($period)] = $count;

            $limitPeriods--;
        }

        // Build the string
        $string = [];
        foreach ($segments as $key => $value) {
            $segment_name = substr($key, 0, -1);
            $segment = $value . ' ' . $segment_name;
            if ($value != 1) {
                $segment .= 's';
            }
            $string[] = $segment;
        }

        return implode(', ', $string);
    }
public static function niceDuration ( $ seconds , $ limitPeriods = 7 , $ zeros = false ) { // Define time periods $ periods = [ 'years' => 31556926 , 'months' => 2629743 , //'weeks'     => 604800, 'days' => 86400 , 'hours' => 3600 , 'minutes' => 60 , 'seconds' => 1 , ] ; // Break into periods $ seconds = ( float ) $ seconds ; $ segments = [ ] ; foreach ( $ periods as $ period => $ value ) { if ( $ limitPeriods >= 1 ) { $ count = floor ( $ seconds / $ value ) ; $ seconds = $ seconds % $ value ; } else { $ count = round ( $ seconds / $ value ) ; $ seconds = 0 ; } if ( ( $ count == 0 && ! $ zeros ) or $ limitPeriods < 1 ) { continue ; } $ segments [ strtolower ( $ period ) ] = $ count ; $ limitPeriods -- ; } // Build the string $ string = [ ] ; foreach ( $ segments as $ key => $ value ) { $ segment_name = substr ( $ key , 0 , - 1 ) ; $ segment = $ value . ' ' . $ segment_name ; if ( $ value != 1 ) { $ segment .= 's' ; } $ string [ ] = $ segment ; } return implode ( ', ' , $ string ) ; }
Get the human string of a duration.

@param int  $seconds      Number of seconds
@param int  $limitPeriods (optional)
@param bool $zeros        Show zeros    (optional; default false)

@return string Nice human duration
Get the human string of a duration .