The point is that you need two valid DateTime
objects to use diff
. From there you can obtain an object DateInterval
with diff
. Then you can take advantage of the properties of that object to make the calculations you need.
For example:
$fechaUno=new DateTime('11:30');
$fechaDos=new DateTime('16:15');
$dateInterval = $fechaUno->diff($fechaDos);
echo $dateInterval->format('Total: %H horas %i minutos %s segundos').PHP_EOL;
You have the exit:
Total: 04 horas 45 minutos 0 segundos
If you check inside your object $dateInterval
you will see what it contains:
We do var_dump($dateInterval);
and we will have:
object(DateInterval)#3 (15) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(0)
["h"]=>
int(4)
["i"]=>
int(45)
["s"]=>
int(0)
["weekday"]=>
int(0)
["weekday_behavior"]=>
int(0)
["first_last_day_of"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
int(0)
["special_type"]=>
int(0)
["special_amount"]=>
int(0)
["have_weekday_relative"]=>
int(0)
["have_special_relative"]=>
int(0)
}
Suppose you now want to calculate how many minutes you have. You apply appropriate calculations to the properties d, h, i
of the object (days, hours, minutes):
$totalMinutos=($dateInterval->d * 24 * 60) + ($dateInterval->h * 60) + $dateInterval->i;
echo "Total minutos: $totalMinutos";
You will have at the exit:
Total minutos: 285
Another way
Another thing you could do would be to obtain the timestamp of the two objects, subtract their value and make operations with it.
Let's see:
$totalSegundos = abs($fechaUno->getTimestamp() - $fechaDos->getTimestamp());
$totalMinutos=$totalSegundos/60;
echo "Total segundos: $totalSegundos";
echo PHP_EOL;
echo "Total minutos: $totalMinutos";
echo PHP_EOL;
echo "Representación Horas,Minutos: ".gmdate("H:i", $totalSegundos);
The output in this case would be:
Total segundos: 17100
Total minutos: 285
Representación Horas,Minutos: 04:45
Cumulative calculation using a loop
Let's assume that your object is similar to this one.
You can create a function that calculates the total of seconds, you accumulate in the variable and at the end you use that total to represent the data as you need it:
/*Simulacro de tu objeto*/
$seguimiento=(object)array(
(object)array("horaInicio"=>"11:30", "horaTermino"=>"16:15"),
(object)array("horaInicio"=>"10:00", "horaTermino"=>"11:00")
);
/*Variable acumulativa*/
$totalSegundos=0;
foreach($seguimiento as $item){
/*Se incrementa cada vez con lo que arroje la función calculateTime*/
$totalSegundos+=calculateTime($item->horaInicio,$item->horaTermino);
}
/*Prueba de datos resultantes*/
echo "Total segundos: $totalSegundos";
echo PHP_EOL;
$totalMinutos=$totalSegundos/60;
echo "Total minutos: $totalMinutos";
echo PHP_EOL;
echo "Representación Horas,Minutos: ".gmdate("H:i", $totalSegundos);
/*Función que es llamada cada vez desde el bucle*/
function calculateTime($horaUno, $horaDos){
$fechaUno=new DateTime($horaUno);
$fechaDos=new DateTime($horaDos);
$diff = abs($fechaUno->getTimestamp() - $fechaDos->getTimestamp());
return $diff;
}
Exit:
Total segundos: 20700
Total minutos: 345
Representación Horas,Minutos: 05:45