<?php
/**
* 当月と前月の祝日を取得
*
* @param string $month 当月
* @param string $beformonth 前月
* @return array
*/
static function get_holidays($month , $beformonth)
{
// 前月1日
$ymd = date($beformonth.'-01');
// 当月末日
$d = new DateTime('last day of ' . $month);
$ymdNext = $d->format('Y-m-d');
// 指定年の祝日をJSON形式で取得するためのURL
$api_key = '取得したAPIキー';
$holidays = array();
$holidays_id = 'japanese__ja@holiday.calendar.google.com';
$url = sprintf(
'https://www.googleapis.com/calendar/v3/calendars/%s/events?'.
'key=%s&timeMin=%s&timeMax=%s&maxResults=%d&orderBy=startTime&singleEvents=true',
$holidays_id,
$api_key,
$ymd.'T00:00:00Z' , // 取得開始日
$ymdNext.'T23:59:59Z' , // 取得終了日
150 // 最大取得数
);
if ( $results = file_get_contents($url, true )) {
// JSON形式で取得した情報を配列に格納
$results = json_decode($results);
// 祝日年月日、祝日名を配列に格納
foreach ($results->items as $item ) {
$date = strtotime((string) $item->start->date);
$title = (string) $item->summary;
$holidays[date('Y-m-d', $date)] = $title;
}
// 祝日の配列を並び替え
ksort($holidays);
}
return $holidays;
}
?>