mercredi 23 septembre 2015

Calculating time difference with values retrieved from MySQL TIME field

I've done a bit of searching and there is a bunch of information on calculating time difference between two times using strtotime('09:00:00) and then putting this against another value.

Currently, I have a database that stores regular hours for staff like so:

+----------+-----------+------------+-------------+--------------+
| staff_id |    day    | start_time | finish_time | total_breaks |
+----------+-----------+------------+-------------+--------------+
|        1 | Monday    | 18:00:00   | 22:00:00    |            0 |
|        2 | Wednesday | 09:00:00   | 17:30:00    |           30 |
+----------+-----------+------------+-------------+--------------+

start_time and finish_time are stored as TIME values in MySQL and total_breaks as an INT which I should probably convert to TIME values as well and represent as 00:30:00 or 00:00:00 for consistency.

What I want to be able to do is display a table that displays all of this information, as well as their total hours worked and then total hours minus any breaks.

I've been able to generate a table with the staff_id, day, start_time and finish_time but am struggling to figure out the best way to calculate the total amount of time worked etc.

I'm using Laravel, and have read that strtotime('00:00:00') is a great way to do this but this won't work as I'm not using a string but rather pulling the time from the database.

What is the best way to calculate the time difference between the two times, and then take in to account the breaks as well.

Have I even set this up correctly? I don't want to set the times up as a DATETIME as I'm just wanting to calculate the time difference of their normal working hours which they do from week to week - although I can always use dummy dates to get this working if necessary.

Any ideas?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire