947740 Posted June 8, 2009 Share Posted June 8, 2009 I have a website set up so that users can clock in and out when they show up for work and leave. I am trying to calculate the total amount of time they spent checked in for the day. I have this data in the database: +-------------+-------+------------+-------+---------------+ | fullname | inout | timestamp | notes | ipaddress | +-------------+-------+------------+-------+---------------+ | ******* | in | 1244483968 | | 192.168.4.1 | | ******* | lunch | 1244501443 | | 192.168.1.127 | | ******* | in | 1244502222 | | 192.168.1.20 | +-------------+-------+------------+-------+---------------+ I have this code: if(date("l",strtotime("now")) == "Sunday") { $start = strtotime("sunday"); $end = strtotime("saturday"); $now = strtotime("sunday"); } else if(date("l",strtotime("now")) == "Saturday") { $start = strtotime("Saturday"); $end = strtotime("Sunday"); $now = strtotime("Saturday"); } else { $start = strtotime("last sunday"); $end = strtotime("saturday"); $now = strtotime(date("n/j/Y",strtotime("now"))); } $query = "SELECT * FROM info WHERE timestamp > $start AND timestamp < $end AND fullname = 'scarruthers'"; $result = mysqli_query($cxn,$query); while($row = mysqli_fetch_assoc($result)) { $name = $row['fullname']; if(!$number[$name]) { $number[$name] = 0; } if(!$total[$name]) { $total[$name] = 0; } echo $total[$name]." seconds<br />"; $names[$name] = $name; if($row['inout'] == "in") { $total[$name] = $total[$name] - $row['timestamp']; } else { $total[$name] = $total[$name] + $row['timestamp']; } $number[$name]++; } echo "final total dump before foreach:".$total['scarruthers']; foreach($names as $name => $value) { if($number[$name] % 2) { $now = strtotime("now"); echo "<Div style='color: red;'>still checked in</div>"; $total[$name] += $now; echo $total[$name]."<br />"; } else { } $hours = $total[$name] / 3600; echo $name." worked ".$total[$name]." seconds OR $hours hours<br />"; } But, the output is 0 seconds -1244483968 seconds 17475 seconds final total dump before foreach:-1244484747 still checked in 9075 ******* worked 9075 seconds OR 2.52083333333 hours That is not right, because the first stamp is from 7:59 this morning...so it should be 6+ hours. I know there is some missing piece of logic in the code...but I keep missing it. Link to comment https://forums.phpfreaks.com/topic/161419-timeclocktimestamp-issue/ Share on other sites More sharing options...
ldougherty Posted June 8, 2009 Share Posted June 8, 2009 How come on your database you have in lunch and then in, there is no out? Link to comment https://forums.phpfreaks.com/topic/161419-timeclocktimestamp-issue/#findComment-851829 Share on other sites More sharing options...
947740 Posted June 8, 2009 Author Share Posted June 8, 2009 How come on your database you have in lunch and then in, there is no out? Because the person in question is still checked in. I have to be able to catch that exception, so I am testing it with still being in. Link to comment https://forums.phpfreaks.com/topic/161419-timeclocktimestamp-issue/#findComment-851833 Share on other sites More sharing options...
947740 Posted June 8, 2009 Author Share Posted June 8, 2009 Just a note...it appears the code ends up calculating the "in" time since the last "out." Link to comment https://forums.phpfreaks.com/topic/161419-timeclocktimestamp-issue/#findComment-851854 Share on other sites More sharing options...
947740 Posted June 9, 2009 Author Share Posted June 9, 2009 *bump* Link to comment https://forums.phpfreaks.com/topic/161419-timeclocktimestamp-issue/#findComment-852304 Share on other sites More sharing options...
947740 Posted June 9, 2009 Author Share Posted June 9, 2009 It turns out that all of my timestamps in the database were 5 hours off...(ahead that is)...so all I had to do for $now was $now = strtotime("now +5 hours")...and the code works. Link to comment https://forums.phpfreaks.com/topic/161419-timeclocktimestamp-issue/#findComment-852323 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.