LoginSignup
12
11

More than 5 years have passed since last update.

【メモ】 PHP で Google カレンダーから祝日を取得

Last updated at Posted at 2013-02-18

個人的なメモ。
Google カレンダーから祝日を取得。

CODE
<?php
    $gCalendarResult = array();
    $holiDays = array();

    // 祝日を調べたい範囲を設定
    $ymd = date('Y-m-d');
    $ymdNext = date('Y-m-t', strtotime('1 month'));

    // 指定年の祝日をJSON形式で取得するためのURL
    $gCalendarUrl = sprintf(
        'http://www.google.com/calendar/feeds/%s/public/full?alt=json&%s&%s',
        'japanese__ja%40holiday.calendar.google.com',
        'start-min=' . $ymd,
        'start-max=' . $ymdNext
    );

    // JSON形式で取得した情報を配列に変換
    $gCalendarResult = json_decode(file_get_contents($url), true);

    // 祝日を取得
    // だたしデータがない場合は飛ばす
    if(isset($gCalendarResult['feed']['entry'])){
        foreach ($gCalendarResult['feed']['entry'] as $value) {
            $holiDay = $value['gd$when'][0]['startTime'];
            $holiDays[$holiDay] = $holiDay;
        }
    }

    // 結果
    print_r($holiDay);
?>
RESULT
/* 上記のコードでの出力例 */
Array
(
    [2013-02-23] => 2013-02-23
    [2013-02-24] => 2013-02-24
    [2013-03-02] => 2013-03-02
    [2013-03-03] => 2013-03-03
    [2013-03-09] => 2013-03-09
    [2013-03-10] => 2013-03-10
    [2013-03-16] => 2013-03-16
    [2013-03-17] => 2013-03-17
    [2013-03-20] => 2013-03-20
    [2013-03-23] => 2013-03-23
    [2013-03-24] => 2013-03-24
    [2013-03-30] => 2013-03-30
    [2013-03-31] => 2013-03-31
)
12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11