
I'm storing users total login hours which I achieve by first storing the login time during the login and then on logout I store the time_out. Then to find total login hours, I use the following code. It all looked fine during the morning hours but now in the afternoon, the total count and time becomes negative.
Here is my login.php code which stores the first login details to hours table:
date_default_timezone_set('America/New_York');
$chk = date("Y-m-d");
$result = mysql_query("SELECT * FROM hours WHERE member_id = '".$_SESSION['MEMBER_ID']."' AND member_name = '".$_SESSION['LOGIN_NAME']."' AND time_in LIKE '%".$chk."%'");
$dt = date("Y-m-d");
if(mysql_num_rows($result) == '0'){
mysql_query("INSERT INTO hours (member_id, member_name, team, time_in) VALUES ('".$_SESSION['MEMBER_ID']."', '".$_SESSION['LOGIN_NAME']."', '".$_SESSION['TEAM']."', '".$dt."')");
}
now the logout.php so update time on logout
$id = $_SESSION['MEMBER_ID'];
date_default_timezone_set('America/New_York');
$dt = date("Y-m-d h:i:s");
$max = mysql_query("SELECT MAX(hours_id) FROM hours WHERE member_id = '".$id."'");
$row = mysql_fetch_row($max);
$sql = "UPDATE hours SET time_out = '".$dt."' WHERE hours_id = '".$row[0]."'";
mysql_query($sql) or die(mysql_error());
And here is how I check a users total login hours for the day or entire month:
<?php
$query = "select member_id, member_name, team, time_in, time_out, sec_to_time(unix_timestamp(time_out) - unix_timestamp(time_in)) AS totalhours from hours where member_id='7'";
$result = mysql_query($query)or die(mysql_error());
$rowNo = 1; //Increment Row Number
$total_time="00:00:00";
while($row = mysql_fetch_assoc($result)){
$time = $row['totalhours'];
$secs = strtotime($time)-strtotime("00:00:00");
$total_time = date("H:i:s",strtotime($total_time)+$secs);
echo "<tr align='left'>";
echo"<td><font color='white'>" .$rowNo++."</font>.</td>";
echo"<td><font color='white'>" .$row['member_name']."</font>.</td>";
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_in']))."</font>.</td>";
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_out']))."</font>.</td>";
echo"<td><font color='white'>" .$time." Hrs</font>.</td>";
echo "</tr>";
}
?>
</table>
<?php echo '<p align="right"><font size="4" color="black">Sub Total:</font> '.$total_time.' Hrs </p>'; ?>
What could be wrong here that after 12noon time is getting negative and all total login hours are displaying wrong?
Note: I'm not using PDO because this is an offline project not online.