Last active
June 15, 2018 08:13
-
-
Save kijtra/be06c07b1d416469108f to your computer and use it in GitHub Desktop.
[PHP] 日本の祝日を取得する関数
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 japan_holiday() { | |
// カレンダーID | |
$calendar_id = urlencode('[email protected]'); | |
// 取得期間 | |
$start = date("Y-01-01\T00:00:00\Z"); | |
$end = date("Y-12-31\T00:00:00\Z"); | |
$url = 'https://www.google.com/calendar/feeds/'.$calendar_id.'/public/basic'; | |
$url .= '?start-min='.$start; | |
$url .= '&start-max='.$end; | |
$url .= '&max-results=30'; | |
$url .= '&alt=json'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
if (!empty($result)) { | |
$json = json_decode($result, true); | |
if (!empty($json['feed']['entry'])) { | |
$datas = array(); | |
foreach ($json['feed']['entry'] as $val) { | |
$date = preg_replace('#\A.*?(2\d{7})[^/]*\z#i', '$1', $val['id']['$t']); | |
$datas[$date] = array( | |
'date' => preg_replace('/\A(\d{4})(\d{2})(\d{2})/', '$1/$2/$3', $date), | |
'title' => $val['title']['$t'], | |
); | |
} | |
ksort($datas); | |
return $datas; | |
} | |
} | |
} | |
/* 取得結果 | |
array ( | |
20140101 => | |
array ( | |
'date' => '2014/01/01', | |
'title' => '元日', | |
), | |
20140113 => | |
array ( | |
'date' => '2014/01/13', | |
'title' => '成人の日', | |
), | |
20140211 => | |
array ( | |
'date' => '2014/02/11', | |
'title' => '建国記念の日', | |
), | |
20140321 => | |
array ( | |
'date' => '2014/03/21', | |
'title' => '春分の日', | |
), | |
20140429 => | |
array ( | |
'date' => '2014/04/29', | |
'title' => '昭和の日', | |
), | |
20140503 => | |
array ( | |
'date' => '2014/05/03', | |
'title' => '憲法記念日', | |
), | |
20140504 => | |
array ( | |
'date' => '2014/05/04', | |
'title' => 'みどりの日', | |
), | |
20140505 => | |
array ( | |
'date' => '2014/05/05', | |
'title' => 'こどもの日', | |
), | |
20140506 => | |
array ( | |
'date' => '2014/05/06', | |
'title' => 'みどりの日 振替休日', | |
), | |
20140721 => | |
array ( | |
'date' => '2014/07/21', | |
'title' => '海の日', | |
), | |
20140915 => | |
array ( | |
'date' => '2014/09/15', | |
'title' => '敬老の日', | |
), | |
20140923 => | |
array ( | |
'date' => '2014/09/23', | |
'title' => '秋分の日', | |
), | |
20141013 => | |
array ( | |
'date' => '2014/10/13', | |
'title' => '体育の日', | |
), | |
20141103 => | |
array ( | |
'date' => '2014/11/03', | |
'title' => '文化の日', | |
), | |
20141123 => | |
array ( | |
'date' => '2014/11/23', | |
'title' => '勤労感謝の日', | |
), | |
20141124 => | |
array ( | |
'date' => '2014/11/24', | |
'title' => '勤労感謝の日 振替休日', | |
), | |
20141223 => | |
array ( | |
'date' => '2014/12/23', | |
'title' => '天皇誕生日', | |
), | |
) | |
*/ |
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 | |
/* | |
* JSON形式が取得できない場合に、iCal形式から取得する | |
* 期間の指定などは不可 | |
* 前後3年分ほどが取得できる | |
*/ | |
function japan_holiday_ics() { | |
// カレンダーID | |
$calendar_id = urlencode('[email protected]'); | |
$url = 'https://calendar.google.com/calendar/ical/'.$calendar_id.'/public/full.ics'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
if (!empty($result)) { | |
$items = $sort = array(); | |
$start = false; | |
$count = 0; | |
foreach(explode("\n", $result) as $row => $line) { | |
// 1行目が「BEGIN:VCALENDAR」でなければ終了 | |
if (0 === $row && false === stristr($line, 'BEGIN:VCALENDAR')) { | |
break; | |
} | |
// 改行などを削除 | |
$line = trim($line); | |
// 「BEGIN:VEVENT」なら日付データの開始 | |
if (false !== stristr($line, 'BEGIN:VEVENT')) { | |
$start = true; | |
} elseif ($start) { | |
// 「END:VEVENT」なら日付データの終了 | |
if (false !== stristr($line, 'END:VEVENT')) { | |
$start = false; | |
// 次のデータ用にカウントを追加 | |
++$count; | |
} else { | |
// 配列がなければ作成 | |
if (empty($items[$count])) { | |
$items[$count] = array('date' => null, 'title' => null); | |
} | |
// 「DTSTART;~」(対象日)の処理 | |
if(0 === strpos($line, 'DTSTART;VALUE')) { | |
$date = explode(':', $line); | |
$date = end($date); | |
$items[$count]['date'] = $date; | |
// ソート用の配列にセット | |
$sort[$count] = $date; | |
} | |
// 「SUMMARY:~」(名称)の処理 | |
elseif(0 === strpos($line, 'SUMMARY:')) { | |
list($title) = explode('/', substr($line, 8)); | |
$items[$count]['title'] = trim($title); | |
} | |
} | |
} | |
} | |
// 日付でソート | |
$items = array_combine($sort, $items); | |
ksort($items); | |
return $items; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment