I'm working on the application where I want to calculate the total late hours of an employee from a set of records between the employee timing shift?

Example Of Employee Time Shift

Shift Start: 01:00:00
Shift End:   09:00:00

Php Code

// example 1
error_reporting('0');
$time1 = "01:00:00";
$time2 = "09:00:00";

date_default_timezone_set('Asia/Karachi');

echo "Time difference: ".get_time_difference($time1, $time2)." hours<br/>";

function get_time_difference($time1, $time2)
{
    $date = date('h:i:s a', time());
    $time1 = strtotime("1/1/1980 $time1");
    $time2 = strtotime("1/1/1980 $time2");
    $totalshifttime = ($time2 -$time1) / 3600;
    $totallate= $totalshifttime - $date ;

return $totallate;

}?

Let's support if ABC employee clock in at 2 pm then should automatically display 1 hour late current function is showing 7 hours late?

Your problem is in line 15 and 16:

  15.  $totalshifttime = ($time2 -$time1) / 3600;
  16.  $totallate= $totalshifttime - $date ;

15 subtracts $time1 from $time2 / 3600 (hrs): 09:00-01:00 = 08:00/3600 = 8.
16 subtracts current time (say 02:00) from 8, not even 8:00 ????

I think what you want is the current time minus time1: ($date - $time1)/3600. if >0 the hours late.

The shift end is never used in deciding if the person was late for shift start. If you want to see hours not worked, you want (shift end - shift start) - (person end - person start). if > 0, the hours and minutes not worked.

HTH,
Sean

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.