I am currently starting a punchclock for our work. This will log employees, In, Lunch Out, Lunch In and Out each day.
I then would like to calculate times each week but specify the week too. So in MySQL lookup all dates in the selected week and then each day calculate the time between IN and OUT minus the lunch.
I can't get my head around how best to do this.
The table looks like:
id | user_id | punch_time | punch_status
The punch status' are:
In = 1
Out = 0
Lunch = 2
This is what i have already which is showing all the results in the past week.
function getTotalHoursThisWeek($week)
{
include("db.php");
$currUser = getUser('id');
if($week == "CURRENT")
{
$query = "SELECT * FROM punch_clock WHERE YEARWEEK(`punch_time`, 1) = YEARWEEK(CURDATE(), 1) && user_id = $currUser";
} else {
$query = "SELECT * FROM punch_clock WHERE YEARWEEK(`punch_time`, 1) = YEARWEEK($week, 1) && user_id = $currUser";
}
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['punch_time']. "<br />";
}
}
This outputs the following:
2015-04-29 17:04:00 - 0
2015-04-29 08:38:03 - 1
2015-04-30 17:20:00 - 0
2015-04-30 09:19:23 - 1
2015-05-01 09:09:47 - 1
2015-05-01 12:36:44 - 2
2015-05-01 12:57:58 - 1
However i would like to then take each day and then calculate the time from In(1) until Out(1) including any entry for Lunch out(2) and then back In(1) for that day.
After that, add them all together to get the total time for that week.