PHPでGoogleのカレンダーより祝日を取得する
コードは結果をみるまで綺麗に書かれていても信用しない
サンプルが書かれていてこんな感じでできますということがあってもやっぱりちょっと不安な感じがする。
カレンダーなどで共通のウェブサービスなどはできるだけ使用した方が良いかと思うので試す。
作ってみる
コード
<?php // API define("CALENDAR_URL", "japanese__ja@holiday.calendar.google.com"); //define("CALENDAR_URL", "japanese@holiday.calendar.google.com"); //define("CALENDAR_URL", "outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar.google.com"); // 日付のタイムゾーンを変更 ini_set("date.timezone", "Asia/Tokyo"); // debug ini_set('xdebug.var_display_max_children', -1); ini_set('xdebug.var_display_max_data', -1); ini_set('xdebug.var_display_max_depth', -1); // 簡単なクラス class CalenderUtil{ public static function getGoogleCalender($min_date, $max_date){ $list = array(); // google apiのurl $url = "http://www.google.com/calendar/feeds/%s/public/full-noattendees?%s"; // パラメータ $params = array( "start-min" => $min_date, "start-max" => $max_date, "max-results" => 50, "alt" => "json", ); $queryString = http_build_query($params); // URLを取得 $getUrl = sprintf($url, CALENDAR_URL, $queryString); // データ取得 if($results = file_get_contents($getUrl)) { // デコードしたデータ $resultsDecode = json_decode($results, true); // 休日を設定するリスト $list = array(); // リスト分出力 foreach ($resultsDecode['feed']['entry'] as $key => $value ) { // 日付 $date = $value['gd$when'][0]['startTime']; // タイトル $title = $value['title']['$t']; // 日付をキーに設定 $list[$date] = $title; } } return $list; } } // 現在の年より年初~年末までを取得 $nowYear = date("Y"); $date1 = date("Y-m-d", strtotime("{$nowYear}0101")); $date2 = date("Y-m-d", strtotime("{$nowYear}1231")); // 出力 $cal = CalenderUtil::getGoogleCalender($date1, $date2); ksort($cal); var_dump($cal);
実行結果
[root@localhost sample01]# php get_ja_holiday.php array(14) { '2013-01-01' => string(6) "元日" '2013-01-14' => string(12) "成人の日" '2013-02-11' => string(18) "建国記念の日" '2013-04-29' => string(12) "昭和の日" '2013-05-03' => string(15) "憲法記念日" '2013-05-04' => string(15) "みどりの日" '2013-05-05' => string(15) "こどもの日" '2013-07-15' => string(9) "海の日" '2013-09-16' => string(12) "敬老の日" '2013-10-14' => string(12) "体育の日" '2013-11-03' => string(12) "文化の日" '2013-11-04' => string(12) "振替休日" '2013-11-23' => string(18) "勤労感謝の日" '2013-12-23' => string(15) "天皇誕生日" } [root@localhost sample01]#
取れます
問題なく取得できました。疑ってごめんなさいm(__)m
そういえば他にもあったのも試す
「japanese@holiday.calendar.google.com」の結果
[root@localhost sample01]# php get_ja_holiday.php array(14) { '2013-01-01' => string(14) "New Year's Day" '2013-01-14' => string(17) "Coming-of-Age Day" '2013-02-11' => string(23) "National Foundation Day" '2013-04-29' => string(9) "Showa Day" '2013-05-03' => string(25) "Constitution Memorial Day" '2013-05-04' => string(12) "Greenery Day" '2013-05-05' => string(14) "Children's Day" '2013-07-15' => string(10) "Marine Day" '2013-09-16' => string(24) "Respect for the Aged Day" '2013-10-14' => string(21) "Health and Sports Day" '2013-11-03' => string(11) "Culture Day" '2013-11-04' => string(14) "Public Holiday" '2013-11-23' => string(23) "Labour Thanksgiving Day" '2013-12-23' => string(22) "The Emperor's Birthday" } [root@localhost sample01]#
全部英語のタイトルになっている。日本語で欲しい場合もあるので保留
「outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar.google.com」の結果
[root@localhost sample01]# php get_ja_holiday.php array(17) { '2013-01-01' => string(34) "元日 / Ganjitsu / New Year's Day" '2013-01-14' => string(47) "成人の日 / Seijin no hi / Coming-of-age Day" '2013-02-11' => string(66) "建国記念の日 / Kenkoku kinen no hi / National Foundation Day" '2013-03-20' => string(49) "春分の日 / Shunbun no hi / Vernal Equinox Day" '2013-04-29' => string(42) "昭和の日 / Sh?wa no hi / Sh?wa Day *" '2013-05-03' => string(62) "憲法記念日 / Kenp? kinenbi / Constitution Memorial Day *" '2013-05-04' => string(47) "みどりの日 / Midori no hi / Greenery Day *" '2013-05-05' => string(44) "子供の日 / Kodomo no hi / Children's Day" '2013-05-06' => string(53) "振替休日 / Furikae ky?jitsu / Substitute Holiday" '2013-07-15' => string(34) "海の日 / Umi no hi / Marine Day" '2013-09-16' => string(54) "敬老の日 / Keir? no hi / Respect for the Aged Day" '2013-09-23' => string(51) "秋分の日 / Sh?bun no hi / Autumnal Equinox Day" '2013-10-14' => string(47) "体育の日 / Taiiku no hi / Health-Sports Day" '2013-11-03' => string(40) "文化の日 / Bunka no hi / Culture Day" '2013-11-04' => string(53) "振替休日 / Furikae ky?jitsu / Substitute Holiday" '2013-11-23' => string(66) "勤労感謝の日 / Kinr? kansha no hi / Labour Thanksgiving Day" '2013-12-23' => string(62) "天皇誕生日 / Tenn? tanj?bi / Emperor Akihito's Birthday" } [root@localhost sample01]#
振替休日も取得していますね。タイトルは日本語、ローマ字、英語かな?
まとめ
PHPでの祝日取得は普通に取得できますね。会社関連の休日などと合わせてなんかカレンダーっぽいのを作成できそう。今まではそれぞれで対応していた感じがしますが、Webサービスとして連携して結構楽できます。
他のデータは別途ダンプで確認する
参考URL
http://memo.dogmap.jp/2012/01/16/google-calendar-japanese-holidays/
http://fdays.blogspot.jp/2008/04/google-calendar-gdatakeygdata.html
http://www.ibm.com/developerworks/jp/xml/library/x-googleclndr/
http://qopl.com/tech_blog/2010/12/%E3%82%AB%E3%83%AC%E3%83%B3%E3%83%80%E3%83%BC%E3%81%AB%E7%A5%9D%E6%97%A5%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B/
http://gomokuankake.com/2013/04/08/3403/