How to Get National Holidays for Egypt and Indonesia

So far I was able to work on PHP national holidays for UK and US using the following links.

http://php.net/manual/en/ref.calendar.php

http://damianoferrari.com/calculating-us-federal-holidays-with-php/

I cannot figure out how to design national holidays for Egypt and Indonesia.

Can anyone point me in the right direction?

I need it to return all the holidays for a given year provided in an array.

public function getHolidayDates($startDate,$endDate){
        $s_y = date('Y',strtotime($startDate) );
        $e_y = date('Y',strtotime($endDate) );
        $holidays = array();

        if($this->site == "uk"){

            for ($i = $s_y; $i < $e_y; $i++) {
                $holidays[] = $this->calculateBankHolidays($i);
            }
            $merged = array();
            foreach($holidays as $value) {
                $merged = array_merge($value,$merged);
            }
            $holidays = $merged;

        }elseif($this->site== "usa"){
            for ($i = $s_y; $i < $e_y; $i++) {
                $us_holidays = new US_Federal_Holidays($i);

                foreach ($us_holidays->get_list() as $value)
                {
                    $holidays[] = date("Y-m-d", $value["timestamp"]);
                }
            }
            //Yii::log(print_r($holidays,true));
        }
        return $holidays;
    }

      

+3


source to share


1 answer


One way to do this would be to grab a list of holidays from somewhere on the internet and add it to your code, then skip it just like it does for US federal holidays. The timeanddate site has holidays for many countries.

http://www.timeanddate.com/holidays/indonesia/

You can try something like this to get a list of holidays

function getHolidays($country){
//Url of Site with list
$url='http://www.timeanddate.com/holidays/'.$country.'/';
//Use curl to get the page
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
# The @ before the method call suppresses any warnings that
# loadHTML might throw because of invalid HTML in the page.
    @$dom->loadHTML($html);
    $holidays=array();
    $items = $dom->getElementsByTagName('tr');
    function tdrows($elements)
    {
        $str = "";
        foreach ($elements as $element)
        {
            $str .= $element->nodeValue . ", ";
        }
        //This pplaces the items into an array 
        $tempArray=explode(',',$str);
        //This gets rid of empty array elements
        unset($tempArray[4]);
        unset($tempArray[5]);
        return $tempArray;
    }
    foreach ($items as $node)
    {
         $holidays[]=tdrows($node->childNodes);
    }
//The first and secone items in the array were the titles of the table and a blank row 
//so we unset them
unset($holidays[0]);
unset($holidays[1]);
//then reindex the array
$holidays = array_values($holidays);
return $holidays;
}

      

With this function you can pass a country to this site and it returns an array containing all the holidays for that country.



You can view an array like this and copy and add to your code.

$indHols=getHolidays('indonesia');
echo "<pre>";
print_r($indHols);
echo "</pre>";

$eqyptHols=getHolidays('egypt');
echo "<pre>";
print_r($indHols);
echo "</pre>";

      

Then you can customize the output array as per your requirement. Obviously, you won't be constantly running this code on every request, which you would only use to grab the array so you can add it to your code.

Although this will probably work for all countries in time and time.

+2


source







All Articles