简体   繁体   中英

Symfony calculate total hours worked

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

Why don't you do your calculation in the Controller and then send the value to Twig instead? Although in Twig, you could simply create a Variable and then add the 9hrs, 2min and 12hrs, 43mins to the variable. But this may be somehow complex because what if you have 9hrs, 45 and 12hrs, 50mins? You would have to do some conversion of (43mins +50mins) into hours again, which, though achievable, could be quite a work.

    <?php           

        $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() );
        }



        $hours      = 0;
        $minutes    = 0;
        $extraHours = 0;
        foreach ($signIns as $signIn) {
            foreach($diff as $dif){
                if($signIn['workerId'] == $user['id'] && $signIn['shiftId'] == $dif['shiftId'] && $dif['workerId'] == $user['id'] ){
                    $hours      += $dif['hoursWorked']['h'];
                    $minutes    += $dif['hoursWorked']['i'];
                }
            }
        }

        if($minutes >= 60){
            $extraHours = intval( $minutes/60 );
            $minutes    =  $minutes%60;
        }

        $realTotal      = ($hours + $extraHours) . "hrs, " . $minutes . "mins";


        return$this->render('users/show.html.twig', [
            'user'              => $user,
            'memos'             => $memos,
            'loggedInUser'      => $loggedInUser,
            'phoneScreenings'   => $phoneScreenings,
            'sites'             => $sites,
            'signIns'           => $signIns,
            'diff'              => $diff,
            'realTotal'         => $realTotal,
        ]);


        // IN TWIG YOU CAN DO JUST...
        {{ realTotal }}

You must calculate it in your controller :

$totalHours = new \DateTime('00:00:00');

foreach ($signIns as $signIn) {
            foreach($diff as $dif){
                if($signIn['workderId'] == $user['id'] && $signIn['shiftId'] == $dif['shiftId'] && $dif['workerId'] == $user['id'] ){
                   $totalHours = self::addtime($totalHours, $dif['hoursWorked']);
                }
            }
        }

And use this function :

public function addtime($time1,$time2)
{
    $interval1 = $time1->diff(new \DateTime('00:00:00')) ;
    $interval2 = $time2->diff(new \DateTime('00:00:00')) ;

    $e = new \DateTime('00:00');
    $f = clone $e;
    $e->add($interval1);
    $e->add($interval2);
    $total = $f->diff($e)->format("%H:%I:%S");
    return new \DateTime($total);
}

Source : Adding two DateTime objects in php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM