Jump to content

Counting amount of Hours based on 2 Rows !?


techiefreak05

Recommended Posts

I have made a system where you enter in your Employee ID and password, and enters a row into the DB. If the last row you entered has han "activityID" of "In", then it enters a new row with the "activityID" of "Out" and vice-versa...

 

What I need to do, is be able to count the amount of time between 2 rows, one IN and one Out. How can I achieve this?

 

Table Setup, "timePunch":

ID, empID, activityID, timeDate

 

ID = number of the row

empID = ID of the employee

activityID = "In", or, "Out"

timeDate = Unix time stamp of time the row was entered.

 

Any help at all would be VERY appreciated!

Link to comment
Share on other sites

Ok, Ive decided to show you an example of what's in my database:

 

 

ID  |  activityID  |  empID  |  timeDate

-------------------------------------------

51          In            13240      1201814815--

                                                                |--I need to be able to combine these 2, and calculate the amount of hours worked.

52          Out          13240      1201819020--

 

 

53          In            13240      1201826696--

                                                                |--I need to be able to combine these 2, and calculate the amount of hours worked.

54          Out          13240      1201826703--

Link to comment
Share on other sites

I have figured out how to connect the 2 rows with each other!

If you are punching in, then create a random string and enters it into the "key" column, but if you are logging out, get the random string from the Punch In row and enter that string into "key" column!

 

Now that they are connected, I just need to figure out how to get the rows based on the key, and then do the math between the 2 rows, then add them all together..

Link to comment
Share on other sites

Personally I think it would be better to structure your table as:

 

ID  | empID  | timeIn  | timeOut

 

Then you can have a query like:

 

SELECT ID,empID,(timeOut - timeIn) as timeDiff FROM table WHERE timeIn NOT NULL AND timeOut NOT NULL

 

Link to comment
Share on other sites

If the data in the table looked like:

 

ID  |  empID  |  timeIn        |  timeOut

-------------------------------------------

51      13240      1201814815      1201818815

52      13240      1201819020      1201824020

53      13240      1201826696      1201826702

54      13240      1201826703

 

The query would return data like this:

ID  |  empID  |  timeDiff

-------------------------------------------

51      13240      4000

52      13240      5000

53      13240      6

 

Record 54 is omitted because there is no timeOut yet. Does that make sense?

 

Link to comment
Share on other sites

This works, for punching in, and then updating the DB for timeOUT

but Im missing something...

When There is already a row with the IN and OUT filled, its supposed to create a new row, with timeIN, but its not..

 

<?php
$now=time();

$cc=mysql_query("SELECT * from tp2 WHERE empID = '$_POST[emp_id]' ORDER BY `ID` DESC LIMIT 1") or die(mysql_error());

if(mysql_num_rows($cc)<1){

//PUNCH ME IN
mysql_query("INSERT INTO `tp2` (`empID`, `timeIN`)
VALUES ('$_POST[emp_id]', '$now');") or die(mysql_error());

header("location: success.php");
exit;

}else{

$r=mysql_fetch_assoc($cc);
$rowID=$r["ID"];
if($r["timeOUT"]==""){
	mysql_query("UPDATE tp2 SET timeOUT = '$now' WHERE ID = '$rowID' AND empID = '$_POST[emp_id]'") or die(mysql_error());
}else{
	mysql_query("INSERT INTO `tp2` (`empID`, `timeIN`)
	VALUES ('$_POST[emp_id]', '$now');") or die(mysql_error());
}

}
?>

 

NEVERMIND.. It worked after I tried a second time... wierd.. well now just to try the diff query, and I owe you my life. =)

Link to comment
Share on other sites

<?php
  $now=time();
  $cc=mysql_query("SELECT * from tp2 WHERE empID = '$_POST[emp_id]' AND timeOut IS NULL ORDER BY `ID` DESC LIMIT 1") or die(mysql_error());
  if(!mysql_num_rows($cc)){
    //PUNCH ME IN
    mysql_query("INSERT INTO `tp2` (`empID`, `timeIN`) VALUES ('$_POST[emp_id]', '$now')") or die(mysql_error());
    header("location: success.php");
    exit;
  }else{
    //PUNCH ME OUT
    $r=mysql_fetch_assoc($cc);
    $rowID=$r["ID"];
    mysql_query("UPDATE tp2 SET timeOUT = '$now' WHERE ID = '$rowID' AND empID = '$_POST[emp_id]'") or die(mysql_error());
  }
?>

Link to comment
Share on other sites

The $timeDiff is the different in seconds. So, to get the difference in minutes, you would divide, by 60 (60 seconds per minute). But if you are looking to get something like the difference between 7pm and 9:15pm is 2hrs 15mins, you would need to look at the remainder:

 

<?php 
$timeDiff=$r["timeDiff"]; //column that was made by subtracting SMALLER unix timestamp from LARGER timestamp.
$hoursDiff = $timeDiff / 3600; //Number of hours in timeDiff
$minsDiff = ($timeDiff % 3600) / 60; //Number of minutes left after hours
echo "{$hoursDiff}hrs {$minsDiff}mins Worked";
?>

Link to comment
Share on other sites

<?php
$totatTimeDiff = 0;
while(...){
  $timeDiff=$r["timeDiff"]; //column that was made by subtracting SMALLER unix timestamp from LARGER timestamp.
  $totalTimeDiff += $timeDIff; //Add to total
  $hoursDiff = $timeDiff / 3600; //Number of hours in timeDiff
  $minsDiff = ($timeDiff % 3600) / 60; //Number of minutes left after hours
  echo "{$hoursDiff}hrs {$minsDiff}mins Worked";
}
$hoursDiff = $totalTimeDiff / 3600; //Total Number of hours
$minsDiff = ($totalTimeDiff % 3600) / 60; //Total Number of minutes left after hours
echo "Total: {$hoursDiff}hrs {$minsDiff}mins Worked";
echo 
?>

 

:)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.