PHP How do I multiply hours with the amount of times it will be repeated?

Asked

Viewed 130 times

1

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

Table Processes

id - description - time

01 - Drilling - 0:01:50

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

echo $tempo * 5;

the variable $tempo, is caught in the processes table.

But get back to me 0, instead of returning 0:09:10 What is wrong ?

2 answers

3


You can do the same solution in two ways.

  1. 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
    
  2. 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
    
  • 1

    Thanks Everson , no mysql da for my process, because one part has 5 holes, other has none, other has more than 1000 holes.

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);

?>

Browser other questions tagged

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