I am currently working on an attendance system for my robotics team using mySQL and PHP using CodeIgniter as a framework. I have functionality for signing in and signing out but the first 2 times the user signs in, it displays their total time by subtracting the length of the current session from 24 hours. It's hard to explain. The output is like this for the first few times the user signs out:
X has signed out
Time: 00 hours 0 minutes 8 seconds Total Time: 23:59:52
It subtracts 8 seconds from 24 hours for some reason for the first 2 times the user signs out.
Here's the code (It's very messy and I apologize, this is my first big project in php)
public function clock_in($pin_number)
{
// Used to get user id
$query = $this->db->get_where('users', array('pin_number' => $pin_number));
$id = $query->row_array()['user_id'];
// Used to get Id of Active event
$query2 = $this->db->get_where('events', array('is_active' => 1));
$event_id = $query2->row_array()['event_id'];
// Used to get last clock of user
$query3 = $this->db->get_where('clocks', array('user_id' => $id));
$clock_array = $query3->result_array();
$size = count($clock_array);
if($size == 0)
{
$data = array(
'user_id' => $id,
'event_id' => $event_id,
'time_stamp' => date('Y-m-d H:i:s'),
'clock_in' => TRUE
);
$this->db->insert('clocks', $data);
echo $this->get_name($pin_number);
echo " Has signed in for the first time <br>";
echo "Welcome to the Team!";
return;
}
$result = $clock_array[$size-1];
$data = array(
'user_id' => $id,
'event_id' => $event_id,
'time_stamp' => date('Y-m-d H:i:s'),
'clock_in' => $this->is_clock_in($id)
);
// Has the user previously clocked in?
if(!$this->is_clock_in($id))
{
//If yes, store the time the user clocked
$time = new DateTime($result['time_stamp']);
//Store the current time
$current = new DateTime(date('Y-m-d H:i:s'));
$difference = $current->diff($time);
$time_a = strtotime($result['time_stamp']);
$time_b = strtotime(date('Y-m-d H:i:s'));
echo $this->get_name($pin_number);
//echo $difference->format('%i')/60;
echo " has signed out<br>";
echo "<br>";
if(abs($time_b-$time_a)/60/60 > 16)
{
echo "You forgot to sign out<br>";
echo "You will not be credited<br>";
echo "You have been automatically signed in";
$data['clock_in'] = TRUE;
$this->db->insert('clocks', $data);
return;
}
echo "Time: ";
//Display how long the user has been signed in
echo $current->diff($time)->format('%H hours %i minutes %s seconds');
$totalTime = new DateTime("0-0-0 0:0:0");
if($size == 0)
{
$totalTime->add($current->diff($time));
}
for($i = 1; $i < $size; ++$i)
{
$row = $clock_array[$i];
$row2 = $clock_array[$i - 1];
if($row['clock_in'] == FALSE)
{
$time_stamp = new DateTime($row['time_stamp']);
echo $time_stamp;
$last_time = new DateTime($row2['time_stamp']);
echo $last_time;
$delta;
if($size == 0)
{
$delta = $last_time->add($time_stamp);
}
else
{
$delta = $last_time->diff($time_stamp);
}
echo $delta;
$totalTime->add($delta);
}
}
$totalTime->add($current->diff($time));
echo "<br>Total Time: ";
echo $totalTime->format('H:i:s');
Any help is greatly appreciated. Thanks.