Function to form hours

Asked

Viewed 38 times

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;

}
  • Have you tried using the date("H:i:s"); ?

2 answers

3

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

$horaFormatada = date('H:i:s', $time);
  • I think that would be the best solution.

1

You have to present it in Datetime format, otherwise it takes the zero, because a zero on the left doesn’t 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;

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.