I am using codeigniter 3.0.3 for this project.
I am trying to get the difference between two timestamps (clock in and clock out).
Here is the function attempting to do so:
public function clock_user_out()
{
$this->db->where('USER_EMAIL', $this->session->userdata('USER_EMAIL'))
$data = $this->db->get('clocked_in_users');
$uemail = $data->row()->USER_EMAIL;
$uclockin = $data->row()->USER_CLOCK_IN;
$uclockout = date(('Y-m-d H:i:s'));
$uhours = $uclockout-$uclockin;
$newdata['USER_EMAIL'] = $uemail;
$newdata['USER_CLOCK_IN'] = $uclockin;
$newdata['USER_CLOCK_OUT'] = $uclockout;
$newdata['USER_WORK_HOURS'] = $uhours;
$this->db->insert('user_hours',$newdata);
$this->db->where('USER_EMAIL', $this->session->userdata('USER_EMAIL'));
$this->db->delete('clocked_in_users');
}
I believe the error is when I am getting the variable $uhours by subtracting $uclockin from $uclockout.
There are 5 columns in the table I am working with.
- ID [int]
- USER_EMAIL [varchar]
- USER_CLOCK_IN [timestamp]
- USER_CLOCK_OUT [timestamp]
- USER_WORK_HOURS [time]
Now for the one I tested these were the results.
- USER_CLOCK_IN = 2015-12-31 07:37:59
- USER_CLOCK_OUT = 2015-12-31 07:38:07
but the result for the USER_WORK_HOURS ended up being 00:20:15. Which does not make sense to me. The only thing that it looks like it did was take the year 2015 and broke it up into the TIME format in the database?
Is that what happened? Am I using the wrong format for that column?
What is the right way to go about doing this?
Thanks in advance.
EDIT: Problem solved thanks to Ashok Chitroda and Jorge Torres here is the corrections I had to make
$uhours = $uclockout-$uclockin;
to
$uhours = date('H:i:s' , (strtotime(date('Y-m-d 00:00:00'))+ strtotime($uclockout) - strtotime($clockin) ));
and also I spelled a variable wrong.
Thank you everyone