日本の祝日を取得する

google

googleカレンダーAPIで日本の祝日を取得していたが有料化に伴いどうしたものか 。

japanese__ja@holiday.calendar.google.com に代わって

holidays-jp
Japanese holidays API / 日本の国民の祝日をJSON形式で返す. holidays-jp has 4 repositories available. Follow their code on GitHub.

を使います。

https://holidays-jp.github.io/api/v1/date.json

自分でdate.jsonを用意してもできます。

今年基準で去年と来年の日本の祝日が取得できるので使えるとおもいます

以下のファイルに休日、祝日を追加すれば2019年以前、2021年以降対応できるかと思います

date.json(jsonファイル)

{
    "2019-01-01": "元日",
    "2019-01-14": "成人の日",
    "2019-02-11": "建国記念の日",
    "2019-03-21": "春分の日",
    "2019-04-29": "昭和の日",
    "2019-04-30": "祝日",
    "2019-05-01": "天皇の即位の日",
    "2019-05-02": "祝日",
    "2019-05-03": "憲法記念日",
    "2019-05-04": "みどりの日",
    "2019-05-05": "こどもの日",
    "2019-05-06": "こどもの日 振替休日",
    "2019-07-15": "海の日",
    "2019-08-11": "山の日",
    "2019-08-12": "山の日 振替休日",
    "2019-09-16": "敬老の日",
    "2019-09-23": "秋分の日",
    "2019-10-14": "体育の日",
    "2019-10-22": "即位礼正殿の儀の行われる日",
    "2019-11-03": "文化の日",
    "2019-11-04": "文化の日 振替休日",
    "2019-11-23": "勤労感謝の日",
    "2020-01-01": "元日",
    "2020-01-13": "成人の日",
    "2020-02-11": "建国記念の日",
    "2020-02-23": "天皇誕生日",
    "2020-02-24": "天皇誕生日 振替休日",
    "2020-03-20": "春分の日",
    "2020-04-29": "昭和の日",
    "2020-05-03": "憲法記念日",
    "2020-05-04": "みどりの日",
    "2020-05-05": "こどもの日",
    "2020-05-06": "憲法記念日 振替休日",
    "2020-07-23": "海の日",
    "2020-07-24": "スポーツの日",
    "2020-08-10": "山の日",
    "2020-09-21": "敬老の日",
    "2020-09-22": "秋分の日",
    "2020-11-03": "文化の日",
    "2020-11-23": "勤労感謝の日",
    "2021-01-01": "元日",
    "2021-01-11": "成人の日",
    "2021-02-11": "建国記念の日",
    "2021-02-23": "天皇誕生日",
    "2021-03-20": "春分の日",
    "2021-04-29": "昭和の日",
    "2021-05-03": "憲法記念日",
    "2021-05-04": "みどりの日",
    "2021-05-05": "こどもの日",
    "2021-07-19": "海の日",
    "2021-08-11": "山の日",
    "2021-09-20": "敬老の日",
    "2021-09-23": "秋分の日",
    "2021-10-11": "スポーツの日",
    "2021-11-03": "文化の日",
    "2021-11-23": "勤労感謝の日"
}

date.jsonを読み込むtest.php

<?php
//上のdate.jsonを読み込むtest.php

$url = "./date.json";
	    if ($results = file_get_contents($url, true))
		{
	        // JSON形式で取得した情報を配列に格納
	        $results = json_decode($results);


	        // 年月日をキー、祝日名を配列に格納

foreach(json_decode(file_get_contents($url), true) as $key => $value) {
	            $date = strtotime((string) $key);
	            $title = (string) $value;
	            $holidays[date('Y-m-d', $date)] = $value;
	        }
	        // 祝日の配列を並び替え
	        ksort($holidays);
	    }

	    return $holidays;
	}

	// 祝日取得
	$national_holiday = japan_holiday($y);

//  print_r($national_holiday);
        //休日
        if(!empty($_GET['date'])){
          $yyy2 = $y;
          $key=$y;
        }else{//値が空なら今年
          $yyy2=date("Y",strtotime("+0 year"));
          $key=$yyy2;
        }
        
        
        foreach($national_holiday as $key => $value ){
          $key2 =$key;
          $key2 = str_replace("年", "-", $key2);
          $key2 = str_replace("月", "-", $key2);
          $key2 = str_replace("日", "", $key2);
          $yyy=substr($key, 0,4);
          
          //$yyy2=date("Y",strtotime("+0 year"));
          
          
          $week = array("日", "月", "火", "水", "木", "金", "土");
          $w = (int)date_format($key2, 'w');
          $datetime = date_create($key2);
          $w = (int)date_format($datetime, 'w');
          $week[$w]="(".$week[$w]."曜日)";
          if($yyy2==$yyy){
            $total0 += 1;
            if (!preg_match('/振替休日/', $value)) {
              //含まれる場合
              $total += 1;
            }
            //$cnt .= count($is);
            echo "<p>".$key.$week[$w]. ":" .$value."</p>"; // 改行しながら値を表示
            $total += $b;
          }
        }
        echo "<h3>祝日は".$total."日あります。(振替休日含めて".$total0."日)</h3>";

前後合わせて3年分。以下サンプルです

2023年令和5年今年の祝日一覧
2023年今年の日本の祝日の一覧表です。

参考
https://matsuoshi.hatenablog.com/entry/2018/11/14/000000

コメント

hage01

2008年頃からワードプレス使ってますヴァージョンは2.6か2.8かだったかな?

hage01をフォローする
タイトルとURLをコピーしました