猿问

laravel 5想自定义全局函数,怎么弄呢?

想把

//生成友好时间形式
function  friendly_date( $from ){
    static $now = NULL;
    $now == NULL && $now = time();
    ! is_numeric( $from ) && $from = strtotime( $from );
    $seconds = $now - $from;
    $minutes = floor( $seconds / 60 );
    $hours   = floor( $seconds / 3600 );
    $day     = round( ( strtotime( date( 'Y-m-d', $now ) ) - strtotime( date( 'Y-m-d', $from ) ) ) / 86400 );
    if( $seconds == 0 ){
        return '刚刚';
    }
    if( ( $seconds >= 0 ) && ( $seconds <= 60 ) ){
        return "{$seconds}秒前";
    }
    if( ( $minutes >= 0 ) && ( $minutes <= 60 ) ){
        return "{$minutes}分钟前";
    }
    if( ( $hours >= 0 ) && ( $hours <= 24 ) ){
        return "{$hours}小时前";
    }
    if( ( date( 'Y' ) - date( 'Y', $from ) ) > 0 ) {
        return date( 'Y-m-d', $from );
    }
    
    switch( $day ){
        case 0:
            return date( '今天H:i', $from );
        break;
        
        case 1:
            return date( '昨天H:i', $from );
        break;
        
        default:
            //$day += 1;
            return "{$day} 天前";
        break;
    }
}

放入函数库,怎么放呢

墨色风雨
浏览 412回答 2
2回答

GCT1015

参考:Creating a Helpers File
随时随地看视频慕课网APP
我要回答