-3

So I have the program saving work start and end times into a database like so:

  id    worker_id                  start                     end
1351            7    2013-06-26 08:00:00     2013-06-26 17:00:00

It saves te times for each day separately, and what I need to do is get the working hours for a worker in a period of time set in a form, for example one month.

Any ideas?

Carlos Campderrós
  • 22,354
  • 11
  • 51
  • 57

1 Answers1

0

Look at SUM() + TIMESTAMPDIFF() functions:

SELECT
    `worker_id`, SUM(TIMESTAMPDIFF(HOUR, `start`, `end`)) as `total`
FROM
    `table`
WHERE
    (`start` BETWEEN 'period-start-date' AND 'period-end-date')
    AND
    (`end` BETWEEN 'period-start-date' AND 'period-end-date')
GROUP BY
    `worker_id`;

Consider adding WHERE clause with constraining dates.

BlitZ
  • 12,038
  • 3
  • 49
  • 68