Jump to content

Timeclock/Timestamp Issue


947740

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.