I'm working on the employee management system where i want to calculate employee late hour after after employee punch in?

For Example

ABC Employee 

Shift Start At : 01:00:00 PM
Shift End   At : 09:00:00 PM
Employee Punch Timing At: 02:30 PM

Now how i calculate employee late hour ?

Php Code

date_default_timezone_set('Asia/Karachi');
$startworkingtime = strtotime('2018/12/3 01:00:00 PM');
$endworkingtime = strtotime('2018/12/3 09:00:00 PM');
$employeecheckintime = strtotime('2018/12/3 02:30:00 PM');

$delta_T = ($endworkingtime- $startworkingtime);
$totallate = $employeecheckintime - $delta_T;   
$hours = round((($delta_T % 604800) % 86400) / 3600); 
$minutes = round(((($delta_T % 604800) % 86400) % 3600) / 60); 
$sec = round((((($delta_T % 604800) % 86400) % 3600) % 60));

echo "Time difference: ".$hours. ' Hours '. $minutes. ' Minutes ' . $sec . ' Seconds ' ."  ";

Dani AI

Generated

Short answer: convert both the scheduled shift start and the punch‑in into timezone‑aware DateTime objects, then compute

late = max(0, punch_in_timestamp - shift_start_timestamp).

Don't subtract the shift length from the punch time (that is why the original math was wrong). As suggested, write the steps first; here they are in plain terms and a compact PHP example.

  • Steps to follow
    • Store/interpret full datetimes (date + time) and a timezone for both the scheduled start and the punch.
    • Use the earliest punch of the day as the check‑in.
    • If punch_in <= shift_start (or within a business grace window) → late = 0.
    • Otherwise late_seconds = punch_in_ts - shift_start_ts; format as H:MM:SS or decimal hours.
    • For overnight shifts, ensure the shift start has the correct date (map punches into the proper shift period).

Example PHP (DateTime + safe formatting):

<?php
date_default_timezone_set('Asia/Karachi');

function calculateLate($shiftStart, $checkIn, $graceMinutes = 0, $tz = 'Asia/Karachi') {
    $tzObj = new DateTimeZone($tz);
    $start = new DateTime($shiftStart, $tzObj);
    $in = new DateTime($checkIn, $tzObj);

    if ($graceMinutes) {
        $start = clone $start;
        $start->modify("+{$graceMinutes} minutes");
    }

    if ($in <= $start) {
        return ['seconds'=>0, 'formatted'=>'0:00:00', 'hours_decimal'=>0.0];
    }

    $s = $in->getTimestamp() - $start->getTimestamp();
    $h = floor($s/3600); $m = floor(($s%3600)/60); $sec = $s%60;
    return ['seconds'=>$s, 'formatted'=>sprintf('%d:%02d:%02d', $h, $m, $sec), 'hours_decimal'=>round($s/3600,2)];
}

// usage example
$late = calculateLate('2018-12-03 13:00:00','2018-12-03 14:30:00');
echo $late['formatted']; // "1:30:00"

Troubleshooting tips: always use a known timezone (or store UTC in DB and convert at display), test edge cases (exactly on time, early, overnight shifts), and apply your company’s grace rules before clamping to zero. To total late time across records, sum the returned seconds and convert once at the end.

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.