Help with logic - PHP

0

I need to do the following calculation:

(tempo protocolo1 * qtd protocolo1) + (tempo protocolo2 * qtd protocolo2)+ (tempo protocolo3 * qtd protocolo3)

So far unsuccessful. Ex:

┌──────────┬──────────┬─────┬──────────┐
│ Protocol │ Tempo    │ Qtd │ Total    │
├──────────┼──────────┼─────┼──────────┤
│ TP1      │ 00:30:00 │  2  │ 01:00:00 │
├──────────┼──────────┼─────┼──────────┤
│ TP2      │ 01:00:00 │  3  │ 03:00:00 │
├──────────┴──────────┼─────┼──────────┤
│                     │Total│ 04:00:00 │
└─────────────────────┴─────┴──────────┘

I can not get there in 4 hours.

<?php

        //hora de producao total
        $acumulador="00:00:00";
        for ($i=0;$i<count($_POST['protocolos']);$i++){


        $qtd_protocolos2 = $_POST['protocolos'][$i];
        $siglas2 = $_POST['siglas'][$i]; //id_protocolo


            //pegar tempo do protocolo atraves da ID (sigla)
             $selecionar_tempo = $pdo->prepare("SELECT tempo_protocolo from protocolo where id_protocolo=:id_pro");
             $selecionar_tempo->bindParam(':id_pro',$siglas2, PDO::PARAM_INT);
             $selecionar_tempo->execute();

             $prot_tempo = $selecionar_tempo->fetch(PDO::FETCH_OBJ);
             $tempo_protocolo = $prot_tempo->tempo_protocolo;


            $tp = strtotime($tempo_protocolo);

            $tp_segundos = strtotime('1970-01-01 '.$tp.'UTC');

            $acum = strtotime($acumulador);

            $acum_segundos = strtotime('1970-01-01 '.$acum.'UTC');


            $acum_segundos =abs((pow($tp,$qtd_protocolos2))+$acum_segundos);



    }
?>
    
asked by anonymous 04.03.2018 / 21:46

2 answers

3

Your calculation is probably making an error because you are using the pow function (which is the exponent's high base) . The correct thing is to make multiplication simple. $tp * $qnt_protocolos2 and thus you get the desired result.

$tp = 3600; // Total de segundo desde 1970-01-01 00:00:00

echo pow( 3600, 3 ); //Saída: 46656000000 (Incorreto) | "pow" retorna (3600 * 3600 * 3600)
echo 3600 * 3;       //Saída: 10800 (Correto)

But ... Here's another way to calculate these values.

You can use the DateTime and DateInterval classes.

The class DateTime , will assist us with the addition, subtraction etc. of dates. The DateInterval class will be responsible for creating a time interval so that we can make the necessary calculations.

  

I will use a simple array , so you will need to adapt it in your project.

Complete and commented code:

<?php

/* Define os protocolos com os tempos o quantidade */
$protocolos = [
    'tp1' => [
        'tempo' => '00:30:00',
        'qnt'   => 2
    ],
    'tp2' => [
        'tempo' => '01:00:00',
        'qnt'   => 3
    ],
];

/* Cria uma data com o "timestamp" 0 */
$date = new DateTime("1970-01-01 00:00:00");

foreach($protocolos as $protocolo => $valor) {

    /**
     * Captura o total de segundos ocorreu  entre
     * '1970-01-01 00:00:00' e '1970-01-01 "tempo_do_protocolo"'.
     */
    $tempo = strtotime("1970-01-01 {$valor["tempo"]}") * $valor["qnt"];

    /**
     * Criamos um intervalo de segundos e somamos com a data da variável indicada
     */
    $date->add( DateInterval::createFromDateString("{$tempo} seconds") );
}

var_dump( $date->format("H:i:s") );

Demonstration at IdeOne

    
04.03.2018 / 22:34
1

You can query directly in the sql query, for example in MySQL:

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(tempo) * $qtd)) FROM protocolo

TIME_TO_SEC() - Converts time in seconds

(tempo * $qtd) - Makes time multiplication (in seconds) times quantity on each line

SUM() - Add the results of all rows

SEC_TO_TIME() - Transforms the second returns into type time (hh: mm: ss)

$qtd would be the variable that comes from the form and tempo the column of the bank

    
04.03.2018 / 23:10