Function to set hours

0

I set the function below that returns me hours in this format 4:5:3 (hour, minute, second).

I would like to return in this format 04:05:03 (hour, minute, second).

function converter($time)
{

    if (is_int($time)) {

        $horas    = floor($time / 3600);
        $minutos  = floor(($time - ($horas * 3600)) / 60);
        $segundos = floor($time % 60);

        $newtime = $horas . ":" . $minutos . ":" . $segundos;

    } else {

        echo "o valor deve ser um inteiro";

    }

    return $newtime;

}
    
asked by anonymous 23.05.2016 / 14:47

2 answers

3

You can format as follows if $time is a timestamp:

$horaFormatada = date('H:i:s', $time);
    
23.05.2016 / 14:57
1

You need to display in DateTime format, otherwise it takes zero, because a leading zero does not count.

function converter($time)
{

if (is_int($time)) {

    $horas    = floor($time / 3600);
    $minutos  = floor(($time - ($horas * 3600)) / 60);
    $segundos = floor($time % 60);


    $newtime= new DateTime();
    $newtime->setTime($horas, $minutos, $segundos);

} else {

    echo "o valor deve ser um inteiro";

}

return $newtime;

}
    
23.05.2016 / 15:02