Language/PHP

PHP 날짜 포맷 출력 유틸 함수

Kim Bob 2019. 9. 10. 17:47
  • 주어진 날짜 문자열을 원하는 포맷으로 출력해주는 함수

    function fmtday( $day, $str )
    {
    	if ( !$day || strlen($day)<8 )
    		return "";
    
    	$Y = substr( $day, 0, 4 );
    	$y = substr( $day, 2, 2 );
    	$m = substr( $day, 4, 2 );
    	$d = substr( $day, 6, 2 );
    	$h = substr( $day, 8, 2 );
    	$M = substr( $day, 10, 2 );
    	$s = substr( $day, 12, 2 );
    
    	$i[0]="일";
    	$i[1]="월";
    	$i[2]="화";
    	$i[3]="수";
    	$i[4]="목";
    	$i[5]="금";
    	$i[6]="토";
    
    	$w = $i[date( 'w',mktime($h,$M,$s,$m,$d,$Y) )];
    
    	$str = str_replace( "Y", $Y, $str );
    	$str = str_replace( "y", $y, $str );
    	$str = str_replace( "m", $m, $str );
    	$str = str_replace( "d", $d, $str );
    	$str = str_replace( "h", $h, $str );
    	$str = str_replace( "M", $M, $str );
    	$str = str_replace( "s", $s, $str );
    	$str = str_replace( "w", $w, $str );
    
    	return $str;
    }
  • 사용 방법

    $str_date = "20190910183027";
    echo fmtday($str_date,"Y/m/d")