[Solved] Reducing arrays of floats from datetime [closed]


So your problem was of using dateinterval object to get the time difference. But them you just add the hours and minutes up but this is “base 60” (as 60 minutes in 1 hour) so you cannot add them up with +.

Example: 1 hour and 15 minute (1.15) plus 1 hour and 45 minute (1.45) are 2.6 numerically speaking but 3 hours (as from your comment).

In order to solve that you need to convert to numerical. You can use the following function:

function convertToDecimal($hours) {
    $parts = explode('.', (string)$hours);
    $min = (count($parts) == 2) ? $parts[1] / 60 : 0;
    return $parts[0] + $min;
}

when calling convertToDecimal(1.15) will return 1.25. add that to you reduce function as:

carry[$item["recruiter"]] += convertToDecimal($item["hours"]);

Hint – asking question properly is not easy task – now that you have your solution try edit your question so it will include all (and only) the data needed to unnderstand your problem and reproduce it.

Better solution include change the way you extract the time difference. Consider the following method:

$shiftStartTime = "2019-01-11T07:00:00";
$shiftEndTime = "2019-01-11T15:15:00";

$startLong = (new DateTime($shiftStartTime))->getTimestamp();
$endLong = (new DateTime($shiftEndTime))->getTimestamp();;

echo ($endLong - $startLong) / (60 * 60);

This will give you 8.25 as you counting the second and not setting the hours using DateInterval.

Hope that helps!

4

solved Reducing arrays of floats from datetime [closed]