Hi guys
I need to calculate a working duration between two datetimes.
Let's say a guy should work on Mon, Tue, Wed, Thi, Fri between
9:00 and 18:00. He started a project Wed at 14:00 and finished
it Mon of the folowing week at 13:00.
I need to know how many hours did he spend on the project.
I have imagined a dates and times tables like
Code:
+------------+
| dates |
+------------+
| ... |
| 2009-03-22 |
| 2009-03-23 |
| 2009-03-24 |
| 2009-03-25 | <- Start
| 2009-03-26 |
| 2009-03-27 |
| 2009-03-28 |
| 2009-03-29 |
| 2009-03-30 | <- End
| 2009-03-31 |
| 2009-04-01 |
| 2009-04-02 |
| 2009-04-03 |
| ... |
+------------+
+-------+
| times |
+-------+
| 01:00 |
| 02:00 |
| 03:00 |
| 04:00 |
| 05:00 |
| 06:00 |
| 07:00 |
| 08:00 |
| 09:00 | <- Working time Start
| 10:00 |
| 11:00 |
| 12:00 |
| 13:00 |
| 14:00 |
| 15:00 |
| 16:00 |
| 17:00 |
| 18:00 | <- Working time End
| 19:00 |
| 20:00 |
| 21:00 |
| 22:00 |
| 23:00 |
| 24:00 |
+-------+
with a query like
Code:
SET start_time DATETIME = "2009-03-25 14:00";
SET end_time DATETIME = "2009-03-30 13:00";
SET working_time_start TIME = "09:00";
SET working_time_end TIME = "18:00";
SET working_day_start INT = 1; // <- Monday
SET working_day_end INT = 5; // <- Friday
SELECT COUNT(DISTINCT CONCAT(dt.date, " ", tm.time)) AS "working_time"
FROM dates AS dt, time AS tm
WHERE CONCAT(dt.date, " ", tm.time) BETWEEN start_time AND end_time
AND tm.time BETWEEN working_time_start AND working_time_end
AND DATE_FORMAT(dt.date, "%w") BETWEEN working_day_start AND working_day_end)
I didn't try the solution and
I'm not sure if it's the best one.
Do someone have a better one ?