開発メモ

開発用のメモです。

PHP n日後が休日の場合は日付を変更する。 (3)

共通化

<?php
require_once CLASS_REALDIR . 'SC_Date.php';

class SC_Date_Ex extends SC_Date {

    public static $arrHoliday2 = NULL;

    /**
    * 休日の判定.
    *
    * @param  integer $year
    * @param  integer $month
    * @param  integer $day
    * @return boolean 休日の場合はtrue
    */
    public function isHoliday( $year, $month, $day ) {
        // 年単位で変更されるため変更
        $this->setArrHoliday2( $year );
        if( is_null( SC_Date_Ex::$arrRegularHoliday ) )
            $this->setRegularHoliday( );

        if( ! empty( SC_Date_Ex::$arrHoliday[ intval( $month ) ] ) ) {
            if( in_array( intval( $day ), SC_Date_Ex::$arrHoliday[ intval( $month ) ] ) ) {
                return true;
            }
        }
        if( ! empty( SC_Date_Ex::$arrRegularHoliday ) ) {
            $day = date( 'w', mktime( 0, 0, 0, $month, $day, $year ) );
            if( in_array( $day, SC_Date_Ex::$arrRegularHoliday ) ) {
                return true;
            }
        }

        return false;
    }

    /**
    * 休日情報をスタティック変数にセット.
    *
    * @return void
    */
    private function setArrHoliday2( $year ) {

        if( is_array( SC_Date_Ex::$arrHoliday2 ) ) {
            if( is_array( SC_Date_Ex::$arrHoliday2[ $year ] ) ) {
                SC_Date_Ex::$arrHoliday = SC_Date_Ex::$arrHoliday2[ $year ];
            }
        }

        $temp = strtotime( $year . '-1-1' );
        $ret = array( );

        $arrHoliday = array(
        // @formatter:off 
                # 土曜日
                "6",
                # 日曜日
                "0",
                # 年始
                $year . "-1-1", 
                $year . "-1-2", 
                $year . "-1-3", 
                # 成人の日(1月2週目 月曜日 8-14日)
                $year . "-1-8-1", 
                $year . "-1-9-1", 
                $year . "-1-10-1", 
                $year . "-1-11-1", 
                $year . "-1-12-1", 
                $year . "-1-13-1", 
                $year . "-1-14-1", 
                # 建国記念日
                $year . "-2-11", 
                #振り替え休日
                $year . "-2-12-1",
                # 昭和の日
                $year . "-4-29",
                #振り替え休日
                $year . "-4-30-1",
                # ゴールデンウィーク
                $year . "-5-3",
                $year . "-5-4",
                $year . "-5-5",
                #振り替え休日
                $year . "-5-6-1",
                #振り替え休日
                $year . "-5-6-2",
                #振り替え休日
                $year . "-5-6-3",
                # 海の日(9月3週目 月曜日 15日-21日)
                $year . "-7-15-1",
                $year . "-7-16-1",
                $year . "-7-17-1",
                $year . "-7-18-1",
                $year . "-7-19-1",
                $year . "-7-20-1",
                $year . "-7-21-1", 
                # 敬老の日(9月3週目 月曜日 15日-21日)
                $year . "-9-15-1",
                $year . "-9-16-1",
                $year . "-9-17-1",
                $year . "-9-18-1",
                $year . "-9-19-1",
                $year . "-9-20-1",
                $year . "-9-21-1",
                # 体育の日(10月2週目 月曜日 8-14日)
                $year . "-10-8-1", 
                $year . "-10-9-1", 
                $year . "-10-10-1",
                $year . "-10-11-1", 
                $year . "-10-12-1", 
                $year . "-10-13-1", 
                $year . "-10-14-1",
                # 文化の日 (11/3)
                $year . "-11-3",
                #振り替え休日
                $year . "-11-4-1",
                # 勤労感謝の日
                $year . "-11-23",
                #振り替え休日
                $year . "-11-24-1",
                # 昭和の日
                $year . "-12-23",
                #振り替え休日
                $year . "-12-24-1",
                # 年末
                $year . "-12-30",
                $year . "-12-31"
            // @formatter:on
        );

        if( $year > 2015 ) {
            # 山の日
            $arrHoliday[ ] = $year . "-8-11";
            $arrHoliday[ ] = $year . "-8-12-1";
        }
        switch($year%4) {
            case 0 :
                # 春分の日
                $arrHoliday[ ] = $year . "-3-20";
                $arrHoliday[ ] = $year . "-3-21-1";
                # 秋分の日
                $arrHoliday[ ] = $year . "-9-22";
                $arrHoliday[ ] = $year . "-9-23-1";
                break;
            case 1 :
            case 2 :
                # 春分の日
                $arrHoliday[ ] = $year . "-3-20";
                $arrHoliday[ ] = $year . "-3-21-1";
                # 秋分の日
                $arrHoliday[ ] = $year . "-9-23";
                $arrHoliday[ ] = $year . "-9-24-1";
                break;
            case 3 :
                # 春分の日
                $arrHoliday[ ] = $year . "-3-21";
                $arrHoliday[ ] = $year . "-3-22-1";
                # 秋分の日
                $arrHoliday[ ] = $year . "-9-23";
                $arrHoliday[ ] = $year . "-9-24-1";
                break;
        }

        while( intval( date( "Y", $temp ) ) == intval( $year ) ) {
            if( in_array( date( "w", $temp ), $arrHoliday ) ) {
                $ret[ intval( date( "m", $temp ) ) ][ ] = intval( date( "d", $temp ) );
            } else if( in_array( date( "Y-n-j", $temp ), $arrHoliday ) ) {
                $ret[ intval( date( "m", $temp ) ) ][ ] = intval( date( "d", $temp ) );
            } else if( in_array( date( "Y-n-j-w", $temp ), $arrHoliday ) ) {
                $ret[ intval( date( "m", $temp ) ) ][ ] = intval( date( "d", $temp ) );
            }
            GC_Utils_Ex::gfPrintLog( var_export( date( "Y-m-d", $temp ), true ), DEBUG_LOG_REALFILE, true );
            GC_Utils_Ex::gfPrintLog( var_export( $ret, true ), DEBUG_LOG_REALFILE, true );
            $temp = strtotime( date( "Y-m-d", $temp ) . " +1 day" );
        }

        if( is_array( SC_Date_Ex::$arrHoliday2 ) ) {
        } else {
            SC_Date_Ex::$arrHoliday2 = array( );
        }
        SC_Date_Ex::$arrHoliday2[ $year ] = $ret;
        SC_Date_Ex::$arrHoliday = $ret;
    }

    /**
    * 定休日情報をスタティック変数にセット.
    *
    * @return void
    */
    private function setRegularHoliday( ) {
        $arrInfo = SC_Helper_DB_Ex::sfGetBasisData( );
        SC_Date_Ex::$arrRegularHoliday = explode( '|', $arrInfo[ 'regular_holiday_ids' ] );
    }

}

処理

<?php
$addDay = 10;
$tmp_date = strtotime( '+' . $addDay . ' day' );
do {
    $tmp_date = strtotime( '+' . $addDay . ' day' );
    $objDate = new SC_Date_Ex( );
    $addDay ++ ;
} while($objDate->isHoliday(intval(date("Y", $tmp_date)), intval(date("m", $tmp_date)), intval(date("d", $tmp_date))));

2015年の結果

<?php
array (
    1 =>
        array (
            0 => 1,
            1 => 2,
            2 => 3,
            3 => 4,
            4 => 10,
            5 => 11,
            6 => 12,
            7 => 17,
            8 => 18,
            9 => 24,
            10 => 25,
            11 => 31,
        ),
    2 =>
        array (
            0 => 1,
            1 => 7,
            2 => 8,
            3 => 11,
            4 => 14,
            5 => 15,
            6 => 21,
            7 => 22,
            8 => 28,
        ),
    3 =>
        array (
            0 => 1,
            1 => 7,
            2 => 8,
            3 => 14,
            4 => 15,
            5 => 21,
            6 => 22,
            7 => 28,
            8 => 29,
        ),
    4 =>
        array (
            0 => 4,
            1 => 5,
            2 => 11,
            3 => 12,
            4 => 18,
            5 => 19,
            6 => 25,
            7 => 26,
            8 => 29,
        ),
    5 =>
        array (
            0 => 2,
            1 => 3,
            2 => 4,
            3 => 5,
            4 => 6,
            5 => 9,
            6 => 10,
            7 => 16,
            8 => 17,
            9 => 23,
            10 => 24,
            11 => 30,
            12 => 31,
        ),
    6 =>
        array (
            0 => 6,
            1 => 7,
            2 => 13,
            3 => 14,
            4 => 20,
            5 => 21,
            6 => 27,
            7 => 28,
        ),
    7 =>
        array (
            0 => 4,
            1 => 5,
            2 => 11,
            3 => 12,
            4 => 18,
            5 => 19,
            6 => 20,
            7 => 25,
            8 => 26,
        ),
    8 =>
        array (
            0 => 1,
            1 => 2,
            2 => 8,
            3 => 9,
            4 => 15,
            5 => 16,
            6 => 22,
            7 => 23,
            8 => 29,
            9 => 30,
        ),
    9 =>
        array (
            0 => 5,
            1 => 6,
            2 => 12,
            3 => 13,
            4 => 19,
            5 => 20,
            6 => 21,
            7 => 23,
            8 => 26,
            9 => 27,
        ),
    10 =>
        array (
            0 => 3,
            1 => 4,
            2 => 10,
            3 => 11,
            4 => 12,
            5 => 17,
            6 => 18,
            7 => 24,
            8 => 25,
            9 => 31,
        ),
    11 =>
        array (
            0 => 1,
            1 => 3,
            2 => 7,
            3 => 8,
            4 => 14,
            5 => 15,
            6 => 21,
            7 => 22,
            8 => 23,
            9 => 28,
            10 => 29,
        ),
    12 =>
        array (
            0 => 5,
            1 => 6,
            2 => 12,
            3 => 13,
            4 => 19,
            5 => 20,
            6 => 23,
            7 => 26,
            8 => 27,
            9 => 30,
            10 => 31,
        ),
) 
Twitter: @asahina_alice