-
-
Save pierrehenri220/6843a969f63131c75c3127ab5ea0701f to your computer and use it in GitHub Desktop.
[PHP] Google Calendar API v3 から祝日取得
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_holidays_this_month($year, $month){ | |
// 月初日 | |
$first_day = mktime(0, 0, 0, intval($month), 1, intval($year)); | |
// 月末日 | |
$last_day = strtotime('-1 day', mktime(0, 0, 0, intval($month) + 1, 1, intval($year))); | |
$api_key = 'YOUR API KEY HERE'; | |
$holidays_id = '[email protected]'; // mozilla.org版 | |
//$holidays_id = '[email protected]'; // Google 公式版日本語 | |
//$holidays_id = '[email protected]'; // Google 公式版英語 | |
$holidays_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, | |
date('Y-m-d', $first_day).'T00:00:00Z' , // 取得開始日 | |
date('Y-m-d', $last_day).'T00:00:00Z' , // 取得終了日 | |
31 // 最大取得数 | |
); | |
if ( $results = file_get_contents($holidays_url) ) { | |
$results = json_decode($results); | |
$holidays = array(); | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment