とりあえず1か月分の祝日を取得します。ついでに振替休日やら、国民の休日にも対応してみました。国民の休日なんてめったにあるもんじゃないんですけど、2015年にまたあるようなので、とりあえず実装してみました。イベントスケジュールとかを作るときに結構便利です。
引数には、年も指定できた方が良いですね。
This file contains 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))); | |
$holidays_url = sprintf( | |
'http://www.google.com/calendar/feeds/%s/public/full-noattendees?start-min=%s&start-max=%s&max-results=%d&alt=json' , | |
'japanese__ja@holiday.calendar.google.com' , | |
date('Y-m-d', $first_day) , // 取得開始日 | |
date('Y-m-d', $last_day) , // 取得終了日 | |
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; | |
} |
[2014-8-26 追記] Google Calendar API v3 に対応させる記事を書きました
[PHP] Google Calendar API v3 で日本の祝日を取得する
コメントを残す