nicklasos
4/18/2014 - 10:47 AM

seconds-human-redable-text.php

<?php

/*
 * Convert seconds to human readable text.
 * Found at: http://csl.sublevel3.org/php-secs-to-human-text/
 *
 */
function secs_to_h($secs)
{
    $units = [
        "week"   => 7*24*3600,
        "day"    =>   24*3600,
        "hour"   =>      3600,
        "minute" =>        60,
        "second" =>         1,
    ];

    // specifically handle zero
    if ( $secs == 0 ) return "0 seconds";

    $s = "";

    foreach ( $units as $name => $divisor ) {
        if ( $quot = intval($secs / $divisor) ) {
            $s .= "$quot $name";
            $s .= (abs($quot) > 1 ? "s" : "") . ", ";
            $secs -= $quot * $divisor;
        }
    }

    return substr($s, 0, -2);
}

$twig->addFilter(new Twig_SimpleFilter('secsToH', function ($secs) use ($app) {
    return secs_to_h($secs);
}));