John Davidson

php - Attendance clock in and out over midnight diffrence

0 comments
Message:


I have an employee attendance system working fine within the same day but if the time passes midnight the total attendance is wrong, for example if my shift starts at 11:00 PM and finishes at 8:00 AM the next day.

Below is part of the code calculating the total hours:


// calculating total work
$total_work = $employee_last_clock_in->diff(new DateTime($data['clock_out']))->format('%H:%I');
$data['total_work'] = $total_work;
$data['clock_in_out'] = 0;

I do have another program simple like (Workday) that calculates correctly and it carrying out the total hours even in 2 different days, here is part of the code.


if($sched_in_time == NULL)
{
$status_in = null;

} else {

$sched_clock_in_time_24h = date("H.i", strtotime($sched_in_time));

$time_in_24h = date("H.i", strtotime($clockin));

if ($time_in_24h <= $sched_clock_in_time_24h)
{
$status_in = trans("In Time");
} else {
$status_in = trans("Late In");
}
}

How I can fix this issue or how I can update my first code to calculate the time even in different days ( after midnight )?



My Answer:
$employee_first_clock_in;

// converting total work to hours and minutes
$total_work_hours = floor($total_work / 3600);
$total_work_minutes = floor(($total_work % 3600) / 60);

echo "Total work: " . $total_work_hours . " hours and " . $total_work_minutes . " minutes";

In order to correctly calculate the total hours worked when the clock in and out times span over midnight, you will need to add a condition to check if the clock out time is before the clock in time. If it is, you will need to add 24 hours to the total work time.

Here is an updated version of the code that takes into account clock in and out times that span over midnight:

// calculating total work
if ($employee_last_clock_in < $employee_first_clock_in) {
$total_work = $employee_last_clock_in + (24*3600) - $employee_first_clock_in;
} else {
$total_work = $employee_last_clock_in - $employee_first_clock_in;
}

// converting total work to hours and minutes
$total_work_hours = floor($total_work / 3600);
$total_work_minutes = floor(($total_work % 3600) / 60);

echo "Total work: " . $total_work_hours . " hours and " . $total_work_minutes . " minutes";

This updated code will correctly calculate the total hours worked even if the clock in and out times span over midnight.

Rate this post

3 of 5 based on 7935 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us