如何获得埃及和印度尼西亚的公共假期

时间:2015-04-28 10:57:10

标签: php calendar

到目前为止,我已经能够使用以下参考资料在英国和美国的PHP国家法定假日中学习。

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

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

我似乎无法了解如何为埃及和印度尼西亚制定国定假日。

有人能指出我正确的方向吗?

我要求它返回数组中提供的给定年份的所有假期。

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;
    }

1 个答案:

答案 0 :(得分:2)

这样做的一种方法是从在线某处获取假期列表并将其添加到您的代码中然后以与美国联邦假期相同的方式循环播放 timeanddate网站为许多国家/地区提供假期。

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

您可以尝试这样的方法来获取假期列表

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;
}

使用该功能,您可以将国家/地区传递到该网站,并返回包含该国家/地区所有假期的数组。

您可以像这样查看数组并将其复制并添加到您的代码中。

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

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

然后,您可以调整输出数组以符合您的要求。 显然,你不会在每次只使用它来获取数组的请求上运行此代码,以便将其添加到代码中。

虽然这可能适用于时间和日期的所有国家。