Answer the question
In order to leave comments, you need to log in
How to calculate working time?
Greetings.
What is the best way to calculate the end date of the time allotted for the employee to resolve the application, taking into account working days, working hours, breaks and short Friday?
Those. we pass to the function:
$startDate = '2019-10-31 10:20:00'; // текущее время
$plusTime = 8; // сколько времени дается на решение заявки
$unit = 1; // 1 - часы, 2 - дни
$workDayStart = '09:00'; // начало рабочего дня
$workDayEnd = '18:00'; // конец рабочего дня
$shortFriday = '16:30'; // конец рабочего дня в пятницу
$startPause = '12:00'; // начало перерыва
$endPause = '13:00'; // окончание перерыва
$tz = 'Europe/Moscow'; // в какой временной зоне производить расчет
echo addTimeToDate($startDate, $plusTime, $unit, $workDayStart, $workDayEnd, $shortFriday, $startPause, $endPause, $tz);
Answer the question
In order to leave comments, you need to log in
Firstly, take out the settings separately, it is unlikely that everything works for you in discord, so $workDayStart and then can be moved to constants or static properties of the helper and not pass 10 parameters there. $unit can also be made an optional parameter with a default value, most likely it is in hours in most cases.
Next - convert hours / days to minutes and count, pure arithmetic, logic is almost zero.
Still, I would, nevertheless, translate all date and time values into Timestamp. This will make it easier to count.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question