Skip to content

Instantly share code, notes, and snippets.

@mojeld
Created April 9, 2016 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojeld/d2dbe2671dedfe1ca477f5e2b6636c3f to your computer and use it in GitHub Desktop.
Save mojeld/d2dbe2671dedfe1ca477f5e2b6636c3f to your computer and use it in GitHub Desktop.
php 日本の休日判断
<?php
#//
#// 休日かどうかを判定
#//
function DateToHolidayBool( $date_str )
{
$date = strtotime( $date_str );
if( isWeekend( $date ) ){ return true; } // 土日判定
if( isNationalHoliday( $date ) ){ return true; } // 祝日判定
//前後が祝日の場合は祝日
if ( isNationalHoliday( strtotime( $date_str . " -1 day" ) ) && isNationalHoliday( strtotime( $date_str . " +1 day" ) ) )
{
return true;
}
//振替休日判定(3日前まで)
$holiday = false;
for( $i = -3; $i < 0; $i++ )
{
$holiday = false;
if( isWeekend( strtotime( $date_str . " " . $i . " day" ) ) )
{
$holiday = true;
for( $j = $i; $j < 0; $j++ )
{
if( ! isNationalHoliday( strtotime( $date_str . " " . $j . " day" ) ) )
{
$holiday = false;
break;
}
}
}
}
return $holiday;
}
#//
#// 土日判定
#//
function isWeekend( $date )
{
if( date('w', $date ) == 6 || date('w', $date ) == 0 )
{
return true;
}
return false;
}
#//
#// 休日判定(国民の休日のみ)
#//
function isNationalHoliday( $date )
{
$holidays = array(
'1.1', //元日
'2.11', //建国記念の日
'4.29', //昭和の日
'5.3', //憲法記念日
'5.4', //みどりの日
'5.5', //こどもの日
'8.11', //山の日
'11.3', //文化の日
'11.23', //勤労感謝の日
'12.23', //天皇誕生日
);
$year = date( 'Y', $date );
$month = date( 'm', $date );
$day = date( 'j', $date );
$week = date( 'w', $date );
foreach( $holidays as $holiday )
{
if( $month.'.'.$day == $holiday )
{
return true;
}
}
if( $month == 1 && $week == 1 && ( 7 < $day && $day < 15 ) ){ return true; } //成人の日(1月第2月曜)
if( $month == 7 && $week == 1 && ( 14 < $day && $day < 22 ) ){ return true; } //海の日(7月第3月曜)
if( $month == 9 && $week == 1 && ( 14 < $day && $day < 22 ) ){return true; } //敬老の日(9月第3月曜)
if( $month == 10 && $week == 1 && ( 7 < $day && $day < 15 ) ){ return true; } //体育の日(10月第2月曜)
if( $month == 3 && $day == shunbun( $year ) ){ return true; } //春分の日
if( $month == 9 && $day == shubun( $year ) ){ return true; } //秋分の日
return false;
}
#//
#// 春分の日取得
#//
function shunbun( $year )
{
if( $year < 1900 || $year > 2099 ){ return 0; }
if( $year % 4 == 0 )
{
return ( $year <= 1956 ) ? 21: ( ( $year <= 2088 ) ? 20 : 19 );
}
elseif( $year % 4 == 1 )
{
return ( $year <= 1989 ) ? 21: 20;
}
elseif( $year % 4 == 2 )
{
return ( $year <= 2022 ) ? 21:20;
}
else
{
return ( $year <= 1923 ) ? 22: ( ( $year <= 2055 ) ? 21: 20 );
}
}
?>
@masagaki
Copy link

masagaki commented Apr 5, 2018

function shubun が見当たらないですね。

@masagaki
Copy link

masagaki commented Apr 5, 2018

56-67行目の $holidays の配列定義は、下記のようにしないと正常に判定されないようです。
例えば、2018-11-30 が祝日と判定されてしまいます。

$holidays = array(
           '01.01',    //元日
           '02.11',    //建国記念の日
           '04.29',    //昭和の日
           '05.03',    //憲法記念日
           '05.04',    //みどりの日
           '05.05',    //こどもの日
           '08.11',    //山の日
           '11.03',    //文化の日
           '11.23',    //勤労感謝の日
           '12.23',    //天皇誕生日
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment