Clue Mediator

Calculate Working Days Excluding Weekends and Holidays in PHP

๐Ÿ“…September 3, 2023
๐Ÿ—PHP

In today's fast-paced world, businesses and individuals often need to calculate the number of working days between two dates, excluding weekends and holidays. Whether it's for payroll processing, project planning, or leave management, having a reliable PHP function to achieve this can be incredibly valuable. In this blog, we will explore a PHP solution to calculate working days, taking into account weekends and custom-defined holiday dates.

Steps to Calculate Working Days Excluding Weekends and Holidays

  1. Setting Up the Environment
  2. The PHP Function - GetWorkingDays
  3. Using the GetWorkingDays Function

1. Setting Up the Environment

Any version of PHP 5.3 or above should work fine. Create a new PHP file, let's call it `working_days.php`, and we can get started.

2. The PHP Function - GetWorkingDays

We will create a function called "GetWorkingDays" that takes two date parameters (start date and end date) and an array of holiday dates as inputs. The function will return the number of working days between the provided dates, excluding weekends and holidays.

working_days.php

<?php
function GetWorkingDays($startDate, $endDate, $holidays = [])
{
    // Convert date strings to UNIX timestamps
    $startTimestamp = strtotime($startDate);
    $endTimestamp = strtotime($endDate);

    // Validate input dates
    if (
        $startTimestamp === false ||
        $endTimestamp === false ||
        $startTimestamp > $endTimestamp
    ) {
        return 0;
    }

    // Number of seconds in a day
    $oneDay = 60 * 60 * 24;

    // Initialize working days count
    $workingDays = 0;

    // Loop through each day between the start and end dates
    for (
        $currentTimestamp = $startTimestamp;
        $currentTimestamp <= $endTimestamp;
        $currentTimestamp += $oneDay
    ) {
        $currentDayOfWeek = date("N", $currentTimestamp); // 1 (for Monday) through 7 (for Sunday)

        // Exclude weekends (Saturday = 6, Sunday = 7)
        if ($currentDayOfWeek < 6) {
            $isHoliday = in_array(date("Y-m-d", $currentTimestamp), $holidays);
            if (!$isHoliday) {
                $workingDays++;
            }
        }
    }

    return $workingDays;
}
?>

3. Using the GetWorkingDays Function

Now that we have our function ready, let's see how we can use it to calculate the number of working days between two dates:

<?php
// Include the working_days.php file here if it's in a separate file

// Test the function with sample data
$startDate = "2023-07-15";
$endDate = "2023-07-31";
$holidays = ["2023-07-20", "2023-07-25"]; // Custom-defined holiday dates

$workingDays = GetWorkingDays($startDate, $endDate, $holidays);

echo "Number of working days between $startDate and $endDate is: $workingDays";
?>

Conclusion

In this blog post, we've explored how to create a simple yet powerful PHP function, "GetWorkingDays", that calculates the number of working days between two dates, excluding weekends and holidays. By incorporating this function into your PHP projects, you can streamline various tasks related to date calculations, project planning, and resource management. Feel free to customize the function to fit your specific requirements and make it even more versatile. Happy coding!