Skip to content
Advertisement

PHP getting TIME difference in hours and minutes format

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

enter image description here

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement