Last active
December 11, 2015 16:49
-
-
Save wokamoto/4630664 to your computer and use it in GitHub Desktop.
[PHP] 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 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment