Error in function to add data type TIME

0

I have the following problem with a function that I use to add two hours, one of the hours comes from a query, which is a TIME type field and the other hour is the one I send for a POST. What I want is to add to the time that the query brought me the hours that arrive by POST, to keep the amount of hours worked in the week:

Ex:

Hours printed in the query 16:00:00

Hours that arrive by POST to add them to the table field: 07:59:00

Result: 23:59:00

Now the problem is when the result goes to 24:00:00, example:

Hour 1: 16:00:00

Hour 2: 08:00:00

The result should be 24:00:00 but what it shows is 00:00:00

The function is as follows:

function SumaHoras($hora1,$hora2){
  $a = new DateTime($hora1);
  $b = new DateInterval((new DateTime($hora2))->format('\P\TH\Hi\Ms\S')); 
  $a->add($b); //Sumo las horas
  return $a->format('H:i:s'); //Imprimo las horas
}

I notice that the problem is when the time passes from 23:59:59 which is when it reaches 24.

What I can think of is that it takes a maximum of 24 hours, I think because of the format I give it in the return.

But I can not find a way to solve it, I would appreciate a help.

    
asked by Alejo Mendoza 21.05.2018 в 07:03
source

1 answer

2

If you want to save a number of hours you should not format it DateTime (), since it gives you the format of 24H and will not let you continue adding, it will restart as a clock would. You can save the hours as a String, or save the total seconds. Greetings.

    
answered by 21.05.2018 в 08:39