PHP | 시간 (몇분전, 몇시간 전, ago) 함수

|
<?php 

function format_date( $time )
{
    $time_difference = time() -$time + 32400;

    if( $time_difference < 1 ) { return '방금 전'; }
    $condition = array( 12 * 30 * 24 * 60 * 60 =>  '년',
                30 * 24 * 60 * 60       =>  '달',
                24 * 60 * 60            =>  '일',
                60 * 60                 =>  '시간',
                60                      =>  '분',
                1                       =>  '초'
    );

    foreach( $condition as $secs => $str )
    {
        $d = $time_difference / $secs;

        if( $d >= 1 )
        {
            $t = round( $d );
            return $t . '' . $str . ( $t > 1 ? '' : '' ) . '전';
        }
    }
}

echo format_date(get_comment_time('U'));

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

클릭하여 복사