PHP How do I multiply hours with how many times it will be repeated?

1

I have a table in my MySQL of products with time, example:

Processes table

  

id - descrision - Time

     

01 - Piercing - 0:01:50

Example if I have to make a part that has 5 holes, I have to multiply quantity by time.

echo $tempo * 5;

The variable $tempo , gets caught in the processes table.

But it returns me 0 , instead of returning 0:09:10 What's wrong?

    
asked by anonymous 21.03.2018 / 18:39

2 answers

3

You can do the same solution in two ways.

  • Directly in MySQL using conversion functions TIME_TO_SEC and SEC_TO_TIME

    SELECT SEC_TO_TIME(
             TIME_TO_SEC('00:01:50') * 5
           ) as TEMPO_TOTAL
    
  • Doing in PHP

    $tempo = "00:01:50"; // Formato: 'HH24:MI:SS'
    $furos = 5;
    
    $str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $tempo);
    sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
    
    // tempo total em segundos
    $tempo_total = ($hours * 3600 + $minutes * 60 + $seconds) * $furos; 
    
    echo gmdate("H:i:s", $tempo_total); //Formata o tempo total para visualização
    
  • 21.03.2018 / 19:11
    2
    <?php
    
    $hora = "04:15:57";
    
    //Transformar em segundos
    $tmp = explode(':', $hora);
    $segundos = $tmp[0] * 3600 + $tmp[1] * 60 + $tmp[2];
    
    //Aplica a multiplicação
    $segundos = $segundos * 5;
    
    //Tranformar em hora
    echo gmdate("H:i:s", $segundos);
    
    ?>
    
        
    21.03.2018 / 19:01