PHP time difference

I am trying to create an attendance system. PHP attendance system

When checking, I call a function that updates the attendance in the database and calculates the hours (using Codeigniter)

public function updateAttendance($data,$id) { date_default_timezone_set('Asia/Karachi'); $attendance=array( 'check_in'=> $data['check_in'], 'check_out'=> $data['check_out'] ); $this->db->WHERE('id',$id)->update('attendance',$attendance); $this->db->query('UPDATE attendance SET hours=(HOUR(TIMEDIFF(check_out,check_in))-1) WHERE DATE(date)=\''.date('Ym-d').'\' and employee_id='.$id); return true; } 

I come to the visitor with the employee ID and look at a view similar to that in the employee profile.

Staff visit

The problem is that I am wrong when calculating the hours, possibly due to my request. Is there a fix on request or do I need to make time in a time format, and then add all of them at the end of the month, and then get the clock from it.

+6
source share
3 answers

You can try this ...

 $to_time = strtotime("2008-12-13 10:42:00"); $from_time = strtotime("2008-12-13 10:21:00"); echo round(abs($to_time - $from_time) / 60,2). " minute"; 
+4
source

I think it is better to use this function:

 $ MY_TIME = strtotime('2017-01-04 23:12'); 

this line of code returns these numbers: 1483571520

you can perform operations with them

and you can return them to a date format for storing in the database using this function:

 date('ymd H:i',$MY_TIME); 
+1
source
 $time = new DateTime($givenTime); $time->diff(new DateTime()); 

Try this code, it may help you better.

+1
source

Source: https://habr.com/ru/post/1013870/


All Articles