Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP calculate half of 8 hour working day

Tags:

php

mysql

moodle

We have Moodle plugin, where we add the travel time of each employee. Until now, we calculated the duration of the travel time in days, as we added the data for every employee only in such form timepickup- 21.10.2105 timereturn-23.10.2015.

The data was being added in the function here:

$user->timepickup = gmdate("Y-m-d H:i", $timepickup);
$user->timereturn = gmdate("Y-m-d H:i", $timereturn);

And the calculating of the travel time here:

$datetime1 = strtotime($employee->timepickup);
$datetime2 = strtotime($employee->timereturn);
$interval = $datetime2 - $datetime1;

As you can see, we add the option to import the exact time in hours and minutes of the timepickup and timereturn.

How to calculate the travel time and display it in days and half days (we assume, that half day are 4 hours from the 8 hour working day.

So instead of travel time 1 day(s) for adding timepickup- 21.10.2105 08:00 timereturn-23.10.2015 12:00, we will have displayed 0,5 day(s).

like image 941
freudsfreund Avatar asked Oct 19 '22 23:10

freudsfreund


1 Answers

Something like this could work

$pickup = strtotime($employee->timepickup);
$return = strtotime($employee->timereturn);
$timediff = ($return - $pickup) / 3600;

$days = floor($timediff / 8);
$halfday = ($timediff - $days * 8) / 4.5;

$days += $halfday < 1 ? 0.5: 1;

It's a bit crude, but you'll end up with adding a half day if the remainder of the time difference is 4.5 hours or less, and adding a whole day if the remainder is larger than 4.5.

like image 83
Jørgen R Avatar answered Oct 22 '22 00:10

Jørgen R