S
S
Sergey Pugovkin2020-10-09 17:28:40
PHP
Sergey Pugovkin, 2020-10-09 17:28:40

How to calculate the working time in a date range?

There are two unix timestamps:

$start = 123456789; // в прошлом
$finish = time(); // текущее

How to calculate the number of seconds between them, but only during working hours.
Those. consider seconds only between 07:00:00 and 22:45:00 inclusive, and only from Monday to Friday inclusive.
What are some ideas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2020-10-09
@Driver86

What are some ideas?
Very easy - you need to calculate the number of working days not including the first and last and multiply by the number of seconds between 07:00:00 and 22:45:00. Then, if the first day is a working day, add the number of seconds between the start of that day (or 07:00:00 if the timestamp is earlier) and 22:45:00. Then, if the last day is a working day, add the number of working seconds in it.
Turning a timestamp into a date, checking what day of the week this date falls on, and adding a day to the date are all simple tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question