Reputation: 43
I'm currently new to PHP , and I am stuck on this problem. I cant seem to subtract the TIME IN
from employee SCHEDULE
function update_dtr($rs) {
foreach ($rs as $key => $value) {
$db=new Database ();
$db->connect();
//date_default_timezone_set('Asia/Ho_Chi_Minh');
$empShift = $db->select(array('myilogin_46.DATE_TIME_RECORDS'),'SHIFT_START'
,"ID = '".$id."' and R_STATUS = 'A'");
$sched= strtotime($empshift);
$alterin = strtotime($value['alterIN']);
if($sched>$alterin){
$late= strtotime("00:00:00");
}else{
//subtract Alterin by the scedule so i can get late in hours and minutes format
$late = $alterin - $sched;
}
$db->update_imp('myilogin_46.DATE_TIME_RECORDS',array(
'date_in'=>date('Y-m-d',strtotime($value['alterIN']))
,'time_in_info'=>'ALTERED IN'
,'time_out_info'=>'ALTERED OUT'
,'time_in'=>date('H:i',strtotime($value['alterIN']))
,'time_out'=>date('H:i',strtotime($value['alterOUT']))
,'date_out'=>date('Y-m-d',strtotime($value['alterOUT']))
,'late'=>gmdate('H:i',strtotime($late))
),"id = '".$value['dtrID']."' and id_emp = '".$value['empID']."' and R_STATUS = 'A'");
$db->disconnect();
PS: I also tried date_diff method, but it doesn't work on me. THis is what my database looks like
Upvotes: 0
Views: 130
Reputation: 43
I got the answer, i did it like this. Example: shiftstart 08:30:00AM and timein= 8:34:00 AM
function update_dtr($rs) {
foreach ($rs as $key => $value) {
$db=new Database();
$db->connect();
$alterin= date('H:i:s', strtotime($value['alterIN']));
$db->select(array('myilogin_46.DATE_TIME_RECORDS'),'SHIFT_START'
,"ID = '".$value['dtrID']."' and ID_EMP = '".$value['empID']."' and
R_STATUS='A'");
foreach ($db->getResult() as $key => $value) {
$shiftstart1 = $value['SHIFT_START'];
$shiftstart= date('H:i:s',strtotime($shiftstart1));
}
var_dump($shiftstart);
var_dump($alterin> $shiftstart);
var_dump($alterin);
//working condition
if($shiftstart >= $alterin){
$late= date('H:i:s',strtotime('00:00:00'));
}else{
$late1=strtotime($alterin)-strtotime($shiftstart);
$late= gmdate('H:i:s', ($late1));
}
// $db->disconnect();
echo $late;
echo date('H:i:s',strtotime($late));
var_dump(date('H:i:s',strtotime($late)));
}
}
OUTPUT SHOULD BE LIKE THIS: 00:04:00
Upvotes: 0
Reputation: 43
It turns out $empshift is not returned as time datatype. When i echoed it , it return the sql query as string itself. So i asked another questioned here: PHP echoed variable returns a mysql query string
Upvotes: 0
Reputation: 526
just use the below function with edited code of yours, also have updated it with additional updates in my function.
function update_dtr($rs) {
foreach ($rs as $key => $value) {
$db=new Database ();
$db->connect();
//date_default_timezone_set('Asia/Ho_Chi_Minh');
$empShift = $db->select(array('myilogin_46.DATE_TIME_RECORDS'),'SHIFT_START'
,"ID = '".$id."' and R_STATUS = 'A'");
$sched= strtotime($empshift);
$alterin = strtotime($value['alterIN']);
if($sched>$alterin){
$late= strtotime("00:00:00");
}else{
//subtract Alterin by the scedule so i can get late in hours and minutes format
$late = datediff($alterin,$sched);
}
$db->update_imp('myilogin_46.DATE_TIME_RECORDS',array(
'date_in'=>date('Y-m-d',strtotime($value['alterIN']))
,'time_in_info'=>'ALTERED IN'
,'time_out_info'=>'ALTERED OUT'
,'time_in'=>date('H:i',strtotime($value['alterIN']))
,'time_out'=>date('H:i',strtotime($value['alterOUT']))
,'date_out'=>date('Y-m-d',strtotime($value['alterOUT']))
,'late'=>$late
),"id = '".$value['dtrID']."' and id_emp = '".$value['empID']."' and R_STATUS = 'A'");
$db->disconnect();
function datediff($d1,$d2)
{
$date1 = new DateTime($d1);
$date2=$date1->format('%H:%I:%S');
$date2 = $date1->diff(new DateTime($d2));
return $date2->format('%H:%I:%S');
}
Upvotes: 1