I need to calculate the total hours worked by guards. I am able to get each shifts' total hours, but I don't know how to add them together. I have a users table, a signIn table and a Shift table. Here is what I got so far.
In the Controller:
$signIns = $em->getRepository('AppBundle:SignIn')
->findAllThisMonthDesc();
foreach ($signIns as $signIn) {
$diff[$signIn->getId()]['signInId'] = $signIn->getId();
$diff[$signIn->getId()]['shiftId'] = $signIn->getShift();
$diff[$signIn->getId()]['workerId'] = $signIn->getWorker();
$diff[$signIn->getId()]['hoursWorked'] = date_diff($signIn->getSignedInAt(), $signIn->getSignedOutAt() );
}
return$this->render('users/show.html.twig', [
'user' => $user,
'memos' => $memos,
'loggedInUser' => $loggedInUser,
'phoneScreenings' => $phoneScreenings,
'sites' => $sites,
'signIns' => $signIns,
'diff' => $diff,
]);
The Twig Template:
{% for signIn in signIns %}
{% for dif in diff %}
{% if signIn.worker.id == user.id and signIn.shift.id == dif.shiftId.id and dif.workerId.id == user.id %}
{{ dif.hoursWorked.h }}hrs, {{ dif.hoursWorked.i }} mins -
{% endif %}
{% endfor %}
{% endfor %}
My output is something like 9hrs and 2 mins - 12hrs and 43 mins. But I want it to be added together so that it displays 21 hrs, 44 mins