PHP calculates half an 8-hour work day

We have Moodle pluginwhere we add travel timeeach employee. So far, we have calculated the duration of the trip in days, since we added data for each employee only in this form timepickup- 10.21.2105 timereturn-23.10.2015.

The data here is added to the function:

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

And the calculation of the trip time here:

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

As you can see, we are adding an option to import exact timeto hoursand minutesfrom timepickupand timereturn.

How to calculate the travel time and display it in daysand half days(we assume that half dayare 4 hoursfrom the working day 8 hour.

, 1 day(s) timepickup - 21.10.2105 08:00 timereturn - 23.10.2015 12:00 0,5 day(s).

+4
1

-

$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;

, , 4,5 , , 4,5.

+2

Source: https://habr.com/ru/post/1613617/


All Articles