How To Calculate Total Working Hours In Php

How To Articles

In this article, I will guide you through the process of calculating total working hours in PHP. As a developer who has worked extensively with PHP, I understand the importance of accurately tracking and calculating working hours for various purposes such as payroll and project management. It’s a fundamental task that every developer should be familiar with.

Before we dive into the implementation details, let’s discuss the requirements and assumptions for our working hour calculation. In this scenario, we will assume that we have a database table named “employees” which stores the details of each employee, including their start time and end time for each working day. We will also assume that the start and end times are stored as UNIX timestamps.

First, we need to retrieve the start and end times for each working day of the employee from the database. We can use SQL queries to fetch this data. Once we have the data, we can loop through each record and calculate the difference between the start and end times for each day.

// Assume $db is the database connection object

// Fetch start and end times from the database
$result = $db->query("SELECT start_time, end_time FROM employees WHERE id = $employeeId");

$workingHours = 0;

// Loop through each record
while ($row = $result->fetch_assoc()) {
$startTime = $row['start_time'];
$endTime = $row['end_time'];

// Calculate the difference in seconds
$diff = $endTime - $startTime;

// Add the difference to the total working hours
$workingHours += $diff;

After iterating through all the records, the variable $workingHours will contain the total working hours in seconds. We can then convert this value to hours and minutes for better readability. Here’s how we can do it:

// Convert the total working hours to hours and minutes
$hours = floor($workingHours / 3600);
$minutes = floor(($workingHours % 3600) / 60);

// Output the result
echo "Total working hours: " . $hours . " hours and " . $minutes . " minutes.";

Now that we have successfully calculated the total working hours in PHP, let’s discuss some additional considerations. It’s important to note that this calculation assumes a 24-hour day and does not take into account any breaks or overtime. If you need to factor in breaks or handle overtime calculations, you will need to modify the code accordingly.

Additionally, it’s worth mentioning that calculating working hours can be influenced by various factors such as time zones, daylight saving time, and employee schedules. It’s crucial to consider these factors and adjust the code accordingly to ensure accurate calculations.


Calculating total working hours is a fundamental task in PHP development. By following the steps outlined in this article, you can accurately calculate the total working hours based on the start and end times stored in a database. Remember to consider additional factors such as breaks and time zones when implementing this functionality in a real-world scenario.