Add time in PHP array

4

I am saving the time in the database with a field of type time . I'm going through an array of data and I want to be able to add them.

I have the following method:

 <?php sumaHoras = 0; ?>
 <?php while ($row = mysqli_fetch_assoc($result)) { ?>

            $value_horario   = $row['total'];
            $parts           = explode(':', $value_horario);
            $resultado       = ($parts[0] + ($parts[1]/6) / 10 . PHP_EOL);
            $sumaHoras       = $sumaHoras + $resultado;
        } ?>
 <?php echo $sumaHoras; ?>

If in my Array $row['total'] comes 3 hours, the sum of those 00:30:00 + 04:30:00 + 01:00:00 results in 1.50 . I should give 7:00 or 07:00:00 .

I've been looking at some codes but none of them has worked for me.

    
asked by MoteCL 31.08.2018 в 22:43
source

5 answers

5

I think the recommended thing is to make the sum in seconds and then convert the total to the desired format, as follows:

$horasbd = [ '00:30:00', '01:00:00', '04:30:00'];

function sumarHoras($horas) {
    $total = 0;
    foreach($horas as $h) {
        $parts = explode(":", $h);
        $total += $parts[2] + $parts[1]*60 + $parts[0]*3600;        
    }   
    return gmdate("H:i:s", $total);
}

echo sumarHoras($horasbd);

//  06:00:00
    
answered by 03.09.2018 / 15:50
source
3

according to what is understood of your needs and complemented with the help of @Oswuell, if you need several hours different amounts, you can do it like this:

$arrayHoras  = array($hora1, $hora2, $hora3);

sumarHoras($horasArray){
  $sumaHoras=0;
  for ($j = 0; $j < count($horasArray) 0; $j++ {

    $value_horario   = $horasArray[$j];
    $parts           = explode(':', $value_horario);
    $resultado      = ($parts[0] + ($parts[1]/6) / 10 . PHP_EOL);
    $sumaHoras = $sumaHoras + $resultado
  }
  return $resultado
}
sumarHoras($arrayHoras);

so you could add as many hours as you want ....

I hope you serve, you tell us!

    
answered by 31.08.2018 в 23:21
1

Try this, I use it and it works perfect

$value_horario   = "$hora1";
$parts           = explode(':', $value_horario);
$res_inicio      = ($parts[0] + ($parts[1]/6) / 10 . PHP_EOL);

$value_horario   = "$hora2";
$parts1_horario  = explode(':', $value_horario);
$res_termino     = ($parts1_horario[0] + ($parts1_horario[1]/6) / 10 . PHP_EOL);

$resultado = $res_inicio + $res_termino;

First you have to delete the ":" to transform them to whole numbers.

    
answered by 31.08.2018 в 22:51
0

I usually use the DateTime () object and create a date by setting the time that interests me, then with the "modify" function I increase the time and extract it with "format".

In this example I only use hours but you can subtract complete dates, minutes and whatever you want in a fairly simple way without complex calculations.

$hora = '13:00';
$fecha = new DateTime('2018-01-24 '.$hora);

$fecha->modify("+1 hour");

$nuevaHora = $fecha->format('H').':00';

echo $nuevaHora;  // Muestra 14:00 y lo guardas o gestionas como necesites

You can combine the format as you are interested, create two dates if you see that it can increase or decrease more than 24 hours and then subtract them to see the difference of hours, etc.

It is interesting to consult the official documentation as it is perfectly explained:

link

link

    
answered by 04.09.2018 в 12:55
0

Probably the cleanest and most elegant solution is to do the sum in the SQL code itself and get the result directly:

SELECT
  SEC_TO_TIME(
    SUM(
      TIME_TO_SEC(total)
    )
  ) suma
FROM tabla;

I start converting the fields TIME in seconds (from the 00:00:00 ) using the function TIME_TO_SEC() . To the total sum of all the records I do the inverse calculation (convert the seconds elapsed from 00:00:00 to hour) by means of the function SEC_TO_TIME() .

Unlike the answer marked as correct, if the sum exceeds 24 hours, the result will be consistent in hours and will not start again from 0.

Your PHP code would be:

<?php
$row = mysqli_fetch_assoc($result);
echo $row['suma'];

You can see a online example here .

    
answered by 04.09.2018 в 13:36