상세 컨텐츠

본문 제목

select box 자주쓰는코드 정리 css 적용 및 php 날짜적용

IT공부방/기타

by 동해둘리 2019. 3. 12. 08:04

본문

반응형

아래와 같은 형태의 날짜선택을 위한 select box 를 CSS 적용하여 기록용으로(개인용)으로 남깁니다



페이지로딩 시 CASE 값을 RECENT 로 넘기게 되면 시작날짜는 3일전 날짜로 세팅이 됩니다 

TODAY 로 넘기게 되면 오늘날짜로 세팅이 됩니다 



        <?
            $tempMonth = "";
            $tempDay = "";

            if ( $CASE == "RECENT" )    $start_year = "";

            if ( $start_year == "" )
            {
                $initYear = date("Y"); $initMonth = date("m"); $initDay = date("d");

                $RECENT_TEMP = mktime(0,0,0, $initMonth, $initDay - 3, $initYear);
                $RECENT_TEMP = date("Ymd",$RECENT_TEMP);

                $initYear = substr($RECENT_TEMP, 0, 4 );
                $initMonth = substr($RECENT_TEMP, 4, 2 );
                $initDay = substr($RECENT_TEMP, 6, 2 );
                
                $start_year = intVal($initYear);
                $start_month = intVal($initMonth);
                $start_day = intVal($initDay);
            }

            if ( $start_month < 10) $start_month = "0" . $start_month;
            if ( $start_day < 10) $start_day = "0" . $start_day;

            $tempMonth = $start_month;
            $tempDay = $start_day;
            
            $START_DATE = $start_year . $tempMonth . $tempDay;

            if ( $end_year == "" ) {
                $end_year = intVal(date("Y")); $end_month = intVal(date("m")); $end_day = intVal(date("d"));
            }

            if ( $end_month < 10)   $end_month = "0" . $end_month;
            if ( $end_day < 10)     $end_day = "0" . $end_day;

            $tempI = $end_month;    $tempI = $end_day;
            
            $END_DATE = $end_year . $end_month . $end_day;

            if ( $CASE == "TODAY" ) {
                $START_DATE = $END_DATE;
                $start_year = $end_year;
                $start_month = $end_month;
                $start_day = $end_day;
            }

        ?>              
        <style>
        input[type=text], select {
            width: 90%;
            height:30px;
            padding: 3px 3px;
            margin: 6px 0;
            display: inline-block;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
            background-color: white;
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
        }
        </style>
        <?
            $sSelectYearStyle = "width:50px;";
            $sSelectMonthStyle = "width:30px;";
            $sSelectDayStyle = "width:30px;";
        ?>
        <select style="margin-left:15px;<?echo $sSelectYearStyle;?>" name = "start_year" size = 1>                  
            <?
                $nStart = 2018; $nEnd = date("Y") + 2;
                for ( $m = $nStart; $m <= $nEnd; $m++ )
                {
                    ?><option value = "<?echo $m;?>" <?if ( $m == $start_year ) { echo " selected "; }?> > <?echo $m;?></option><?
                }
            ?>
        </select>년
        <select style="<?echo $sSelectMonthStyle;?>" name = "start_month" size = 1>
            <?
                for ( $i = 1; $i <=12; $i++ )
                {
                    if ( $i < 10 ) { $tempI = "0" . $i; }   else { $tempI = $i; }
                    
                    ?><option <?if ( $tempI == $start_month ) { echo " selected "; }?> value = "<?echo $i;?>"> <?echo $tempI;?></option><?
                }
            ?>
        </select>월          
        <select style="<?echo $sSelectDayStyle;?>" name = "start_day" size = 1>
            <?
                for ( $i = 1; $i <=31; $i++ )
                {
                    if ( $i < 10 ) { $tempI = "0" . $i; }   else { $tempI = $i; }
                    
                    ?><option <?if ( $tempI == $start_day ) { echo " selected "; }?> value = "<?echo $i;?>"> <?echo $tempI;?></option><?
                }
            ?>
        </select>일~<select style="<?echo $sSelectYearStyle;?>" name = "end_year" size = 1
            <?
                $nStart = 2018; $nEnd = date("Y");
                for ( $i = $nStart; $i <= $nEnd; $i++ )
            ?>
            <option value = "<?echo $i;?>" <?if ( $i == $end_year ) { echo " selected "; }?> ><?echo $i;?></option>
        </select>년
        <select style="<?echo $sSelectMonthStyle;?>" name = "end_month" size = 1>
            <?
                for ( $i = 1; $i <=12; $i++ )
                {
                    if ( $i < 10 ) { $tempI = "0" . $i; } else { $tempI = $i; }
                    
                    ?><option <?if ( $tempI == $end_month ) { echo " selected "; }?> value = "<?echo $i;?>"><?echo $tempI;?></option><?
                }
            ?>
        </select>월
        <select style="<?echo $sSelectDayStyle;?>" name = "end_day" size = 1>
            <?
                for ( $i = 1; $i <=31; $i++ )
                {
                    if ( $i < 10 ) { $tempI = "0" . $i; } else { $tempI = $i; }                    
                    ?><option <?if ( $tempI == $end_day ) { echo " selected "; }?> value = "<?echo $i;?>"><?echo $tempI;?></option><?
                }
            ?>
        </select>일




날짜 계산하는 함수가 더 좋은 함수가 있었네요


 date_interval_create_from_date_string()  


    // 7일전 날짜부터 검색대상
    if ( $start_year == "" )
    {
        $start_date = date_create($ArgSelectedDate);
        date_add($start_date, date_interval_create_from_date_string('-7 days'));

        $start_year = date_format($start_date, 'Y');
        $start_month = date_format($start_date, 'm');
        $start_day = date_format($start_date, 'd');

        $end_year = date("Y");
        $end_month = date("m");
        $end_day = date("d");
    }





반응형

관련글 더보기

댓글 영역