1. 程式人生 > >常用評論時間描述

常用評論時間描述

1、常用評論時間描述(不知道來自哪裡了,哈哈,見諒哈)

//評論常用時間術語
function mdate($time = NULL) {
    $text = '';
    $time = $time === NULL || $time > time() ? time() : intval($time);
    $t = time() - $time; //時間差 (秒)
    $y = date('Y', $time)-date('Y', time());//是否跨年
    switch($t){
        case $t == 0:
            $text = '剛剛';
            break;
        case $t < 60:
            $text = $t . '秒前'; // 一分鐘內
            break;
        case $t < 60 * 60:
            $text = floor($t / 60) . '分鐘前'; //一小時內
            break;
        case $t < 60 * 60 * 24:
            $text = floor($t / (60 * 60)) . '小時前'; // 一天內
            break;
        case $t < 60 * 60 * 24 * 3:
            $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
            break;
        case $t < 60 * 60 * 24 * 30:
            $text = date('m月d日 H:i', $time); //一個月內
            break;
        case $t < 60 * 60 * 24 * 365&&$y==0:
            $text = date('m月d日', $time); //一年內
            break;
        default:
            $text = date('Y年m月d日', $time); //一年以前
            break;
    }

    return $text;
}