サンプル・プログラム:実行例

サンプル・プログラム
getHolidayGoogle.php | サンプル・プログラム本体 |
pahooInputData.php | データ入力に関わる関数群。 使い方は「数値入力とバリデーション」「文字入力とバリデーション」などを参照。include_path が通ったディレクトリに配置すること。 |
バージョン | 更新日 | 内容 |
---|---|---|
2.1.0 | 2025/02/11 | pahooInputData.php 導入,など |
2.0.1 | 2024/09/23 | 参考URL変更 |
2.0 | 2022/01/10 | 表示改良,PHP8対応,リファラ・チェック改良 |
1.0 | 2013/05/24 | 初版 |
バージョン | 更新日 | 内容 |
---|---|---|
1.8.0 | 2024/11/12 | validRegexPattern() 追加 |
1.7.0 | 2024/10/09 | validURL() validEmail() 追加 |
1.6.0 | 2024/10/07 | isButton() -- buttonタグに対応 |
1.5.0 | 2024/01/28 | exitIfExceedVersion() 追加 |
1.4.2 | 2024/01/28 | exitIfLessVersion() メッセージ修正 |
Google Calendar API v3
入力パラメータ(IN)はGETで渡し、出力結果(OUT)はjson形式で戻るというAPIである。

calendarIDは、Googleが公式に用意している日本語カレンダー ja.japanese#holiday@group.v.calendar.google.com を利用する。2025年(令和7年)2月現在、国民の祝日だけでなく、銀行休業日なども祝日として取得する仕様に変わっている。
URL |
---|
https://www.googleapis.com/calendar/v3/calendars/{calendarId}/events |
{calendarId} = urlencode('ja.japanese#holiday@group.v.calendar.google.com') |
フィールド名 | 要否 | 内 容 |
---|---|---|
key | 必須 | Google API key |
timeMin | 必須 | 取得開始日時。RFC3339形式。 |
timeMax | 必須 | 取得終了日時。RFC3339形式。 |
maxResults | 任意 | 取得する最大件数。 |
orderBy | 任意 | 並び順。 |
singleEvents | 任意 | シングル・イベント。TRUE/FALSE |
Google API key
getHolidayGoogle.php
63: // Google API key(自分のキーを設定すること)
64: define('GOOGLE_API_KEY', '*********************************');
解説:Google Calendar API呼び出し
getHolidayGoogle.php
219: /**
220: * Google Calendar APIを使って1年分の祝日を取得
221: * @param int $year 西暦年
222: * @param array $holidays 祝日を格納する配列
223: * @param string $url WebAPIのURL格納用
224: * @return int 格納した祝日数
225: */
226: function getHoliday_by_Google($year, &$holidays, &$url) {
227: $type = urlencode('ja.japanese#holiday@group.v.calendar.google.com');
228: $google_api_key = GOOGLE_API_KEY;
229: $start = sprintf("%04d-01-01T00:00:00Z", $year);
230: $finish = sprintf("%04d-01-01T00:00:00Z", $year + 1);
231: $url = "https://www.googleapis.com/calendar/v3/calendars/{$type}/events?key={$google_api_key}&timeMin={$start}&timeMax={$finish}&maxResults=50&orderBy=startTime&singleEvents=true";
232:
233: $cnt = 0;
234: $result = file_get_contents($url);
235: $result = json_decode($result);
236: if (! empty($result->items)) {
237: foreach ($result->items as $value) {
238: $title = (string)$value->summary;
239: $date = (string)$value->start->date;
240: $holidays[$date] = $title;
241: $cnt++;
242: }
243: }
244:
245: return $cnt;
246: }
戻り値はJSON形式であるが、PHPには関数 json_decode が組み込まれているので、これを使って、個々の祝日に分解する。

プログラムのその他の部分は、「PHPで祝日を求める」で使ったものとほぼ同じである。
参考サイト
- Google Calendar API
- PHPで祝日を求める:ぱふぅ家のホームページ
(2025年2月11日)pahooInputData.php 導入,など