# phpで日本の祝日や振替休日を出力する。

- リンク: https://torounit.com/blog/php%e3%81%a7%e6%97%a5%e6%9c%ac%e3%81%ae%e7%a5%9d%e6%97%a5%e3%82%84%e6%8c%af%e6%9b%bf%e4%bc%91%e6%97%a5%e3%82%92%e5%87%ba%e5%8a%9b%e3%81%99%e3%82%8b%e3%80%82/
- 公開日: 2013-01-25T10:00:59+09:00
- 作成者: Toro\_Unit

いまさらですが、あけましておめでとうございます。[HTML5KARUTA](http://roadstohtml5.com/html5karuta/)
に参加させていただいたり、引っ越しがあったりでブログがろくに書けませんでした。今年
もよろしくお願いします。

そして、今回の内容ですが、元ネタは[ Google Calendar API から日本の祝日データを取得 | memo.dogmap.jp](http://memo.dogmap.jp/2012/01/16/google-calendar-japanese-holidays/)
です。

    ```decode:true
    function get_holidays_this_month($month){

        $holidays_url = sprintf(
                'http://www.google.com/calendar/feeds/%s/public/full-noattendees?start-min=%s&amp;amp;start-max=%s&amp;amp;max-results=%d&amp;amp;alt=json' ,
                'japanese__ja@holiday.calendar.google.com' ,
                '2013-'.$month.'-01' ,  // 取得開始日
                '2013-'.$month.'-31' ,  // 取得終了日
                31            // 最大取得数
                );
        if ( $results = file_get_contents($holidays_url) ) {
                $results = json_decode($results, true);
                $holidays = array();
                foreach ($results['feed']['entry'] as $val ) {
                        $date  = $val['gd$when'][0]['startTime'];
                        $week = date('w',strtotime($date));
                        $title = $val['title']['$t'];
                        $holidays[$date] = $title;

                        if( $week == 0) {
                            $nextday = date('Y-m-d',strtotime('+1 day', strtotime($date)));
                            $holidays[$nextday] = '振替休日';
                        }

                        $before_yesterday = date('Y-m-d',strtotime('-2 day', strtotime($date)));

                        if(isset($holidays[$before_yesterday])){
                            $yesterday = date('Y-m-d',strtotime('-1 day', strtotime($date)));
                            $holidays[$yesterday] = '国民の休日';
                        }

                }
                ksort($holidays);
        }
        return $holidays;
    }
    ```

とりあえず1か月分の祝日を取得します。ついでに振替休日やら、国民の休日にも対応して
みました。国民の休日なんてめったにあるもんじゃないんですけど、2015年にまたあるよう
なので、とりあえず実装してみました。イベントスケジュールとかを作るときに結構便利
です。

追記：[http://memo.dogmap.jp/2013/01/25/re-google-calendar-japanese-holidays/](http://memo.dogmap.jp/2013/01/25/re-google-calendar-japanese-holidays/)

確かに・・・・
