EDDY
EDDY

Reputation: 103

How to get total sum of hours for each day

I have a situation where I need to calculate the total of hours for each sets on each day. But I have stuck many days on it. I try to get output as below:

Day        Clock In  Clock Out  Total Hours(Hr)   Total Hours Per Day(Hr)
Monday     08:00     09:00      1.00                                     
           10:00     12:00      2.00          
           13:00     14:30      1.50              4.50
Tuesday    08:30     10:00      1.50
           15:00     17:50      2.83              4.33
Wednesday  08:00     17:00      9.00              9.00

                                     Total       17.83

I have tried to code and do some effort this far. Kindly help me guys. Will appreciate it so much.

for($days=0;$days<3;$days++) { //get how many days

$clocked_records_each_day = count($get_attendance_records); //get attendance                 clock sets

for($records=0; $records<$clocked_records_each_day; $records++) {
$total_hour_per_set = $clocked_records_each_day[$records]['total_hour'];
$total_hour_per_day = ??;
$total_hour_overall = ??;
//stuck until here
//get the output and display
}

}

Upvotes: 0

Views: 101

Answers (2)

Simon Goodman
Simon Goodman

Reputation: 1204

You need to do it in two parts, first sum it all up, then display it.

for($records=0; $records<$clocked_records_each_day; $records++) {
  $total_hour_per_set = $clocked_records_each_day[$records]['total_hour'];
  $total_hour_per_day[$clocked_records_each_day[$records]['day'][] =  $total_hour_per_set
  $total_hour_overall+= $total_hour_per_set;
}

So now you have all the data you need.

And for each day you can display

for($records=0; $records<$clocked_records_each_day; $records++) {
  Display( $total_hour_per_set = $clocked_records_each_day[$records]['total_hour']);
  Display( array_sum( $total_hour_per_day[$clocked_records_each_day[$records]['day'] ) );
  Display( $total_hour_overall+= $total_hour_per_set );
}

This assumes that you have a Display( ... ) function.

Upvotes: 1

fusion3k
fusion3k

Reputation: 11689

Basically:

/* Init grand total:  */
$total_hour_overall = 0;

for($days=0;$days<3;$days++) { //get how many days

    /* Init day subtotal at each for loop: */
    $total_hour_per_day = 0;

    $clocked_records_each_day = count($get_attendance_records); //get attendance                 clock sets

    for($records=0; $records<$clocked_records_each_day; $records++) {
        $total_hour_per_set = $clocked_records_each_day[$records]['total_hour'];

        /* Increment day subtotal and grand total: */
        $total_hour_per_day += $clocked_records_each_day[$records]['total_hour'];
        $total_hour_overall += $clocked_records_each_day[$records]['total_hour'];

        /* Echo set total inside records for:  */
        echo $total_hour_per_set;
    }

    /* Echo day subtotal inside days for:  */
    echo $total_hour_per_day;
}

/* Echo grand total outside for loop:  */
echo $total_hour_overall;

Upvotes: 1

Related Questions