Get all public holidays date between two dates and hold in array in PHP Procedural

  php

Anyone can help me to get all public holidays date between 2 dates?

I have a code to count the number of working days then I’m here asking for help on how to
get all public holidays between 2 date (the $given_date and $target_date) and hold it in array for me to replace the hard coded value of $holiday variable.

<?php

function getWorkingDays($startDate, $endDate, $offdays, $holidays)
{
    $endDate = strtotime($endDate);
    $startDate = strtotime($startDate);
    $days = ($endDate - $startDate) / 86400 + 1;
    $counter = 0;
    for($i = 1; $i <= $days; $i++)
    {
        $the_first_day_of_week = date("N", $startDate);
        $startDate += 86400;
        if(!in_array($the_first_day_of_week, $offdays) && !in_array(date("Y-m-d",$startDate), $holidays))
        {
            $counter++;
        }
    }   

    return $counter;
}

$given_date = "2021-10-20";
$target_date = "2021-11-06";
$holidays = array("2021-11-01", "2021-11-02");
$offdays = array(5,6);
echo getWorkingDays($given_date, $target_date, $offdays, $holidays);

?>

Source: Ask PHP

LEAVE A COMMENT