Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save TaffarelXavier/8308d951ffb937c47d35030214bca7f2 to your computer and use it in GitHub Desktop.

Select an option

Save TaffarelXavier/8308d951ffb937c47d35030214bca7f2 to your computer and use it in GitHub Desktop.
Converter soma de minutos em formato de horas (ex: 70min = 01:10) em php

Converter soma de minutos em formato de horas (ex: 70min = 01:10) php

function converterHoras($tempos)
{
    $hor = 0;
    $min = 0;

    foreach ($tempos as $tempo) {
        $hora_inicial = Carbon::createFromFormat('H:i', $tempo);
        $hor += $hora_inicial->hour * 60;
        $min += $hora_inicial->minute;
    }
    $total_minutos = $hor + $min;
    $horas = floor($total_minutos / 60);
    $minutos_restantes = $total_minutos % 60;

    $textoHoras = str_pad($horas, 2, '0', STR_PAD_LEFT);
    $textoMinutos = str_pad($minutos_restantes, 2, '0', STR_PAD_LEFT);
    return "$textoHoras:$textoMinutos";
}

Exemplo:

converterHoras(['1:00','2:00','0:26','0:34','1:30'])

// Saída:5h30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment