PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

PHP Calculating total hours not getting accurate answer

I am trying to calculate the total number of a person based on when they in, break, resume, and out.

I have these data:

$data[0]['type'] = 'in';
$data[0]['created_at'] = '2019-02-01 08:01:52';

$data[1]['type'] = 'break';
$data[1]['created_at'] = '2019-02-01 12:00:40';

$data[2]['type'] = 'resume';
$data[2]['created_at'] = '2019-02-01 13:00:39';

$data[3]['type'] = 'break';
$data[3]['created_at'] = '2019-02-01 14:29:21';

$data[4]['type'] = 'resume';
$data[4]['created_at'] = '2019-02-01 14:29:50';

$data[5]['type'] = 'break';
$data[5]['created_at'] = '2019-02-01 14:29:53';

$data[6]['type'] = 'resume';
$data[6]['created_at'] = '2019-02-01 14:30:00';

$data[7]['type'] = 'break';
$data[7]['created_at'] = '2019-02-01 14:30:09';

$data[8]['type'] = 'resume';
$data[8]['created_at'] = '2019-02-01 14:30:12';

$data[9]['type'] = 'out';
$data[9]['created_at'] = '2019-02-01 18:01:51';

$hours = 0;
$minutes = 0;
foreach($data as $key => $d){
    $d['created_at'] = str_replace(" ","T",$d['created_at']);
    if($d['type'] == 'in' || $d['type'] == 'resume'){
        $workingTime = new DateTime($d['created_at']);
    }

    if($d['type'] == 'break' || $d['type'] == 'out'){
        $date2 = new DateTime($d['created_at']);
        $diff = $date2->diff($workingTime);
        $tempHours = $diff->h;

        $tempHours = $tempHours + ($diff->days*24);
        $hours += $tempHours;

        $tempMin = $diff->i;
        $minutes  += $tempMin;
    }
}

if($minutes == 60){
    $hours += 1;
    echo $hours. " hours and 00 minutes";
}else if($minutes > 60){
    $hours = $hours + floor($minutes/60);
    $minutes = $minutes % 60;
    echo $hours. " hours and ".$minutes." minutes";
}else{
    echo $hours. " hours and ".$minutes." minutes";
}

The total that I am getting is: 8 hours and 57 minutes , while the correct answer should be 08 hour and 59 minutes and 21 seconds.

I think the reason why I am not getting the correct answer it's because in my code I do not include the seconds in the calculation.

Can someone help me in getting the correct answer by including the seconds in the calculation.

Your help will be greatly appreciated! Thank you.

By the way, the reason why i know that the correct answer is 08 hour and 59 minutes and 21 seconds because of this website: https://www.calculator.net/time-calculator.html

I get the sum of the difference of all of these:

$a = break - in
$b = break - resume
$c = out - resume
Then: $total_time = $a + $b + $c

Let me know if this is unclear. Thank you.

Upvotes: 0

Views: 61

Answers (1)

Nick
Nick

Reputation: 147206

This code will do what you want. It processes each element of the array, setting the work start and end time when we see in; setting a period start every time we see in or resume; and adding the period length to the day's work time every time we see out or break. Finally we take the difference between the day's start time and the time which we have created by adding all the periods, which gives us the whole day's work time.

foreach ($data as $event) {
    $this_time = new DateTime($event['created_at']);
    switch ($event['type']) {
        case 'in':
            $work_start_time = clone $this_time;
            $work_end_time = clone $this_time;
        case 'resume':
            $start_time = $this_time;
            break;
        case 'break':
        case 'out':
            $work_end_time->add($start_time->diff($this_time));
            break;
    }
}
$working_hours = $work_start_time->diff($work_end_time);
echo $working_hours->format('%H hours, %i minutes, %s seconds');

Output:

08 hours, 59 minutes, 21 seconds

Note

If a shift can be longer than a day, you will need to change the last line to this:

echo $working_hours->format('%a days, %H hours, %i minutes, %s seconds');

Or if you need to have only hours, not days, use this:

echo $working_hours->days*24+$working_hours->h . $working_hours->format(' hours, %i minutes, %s seconds');

Demo on 3v4l.org

Upvotes: 2

Related Questions