/** * 友好的時間顯示 * * @param int $sTime 待顯示的時間 * @param string $type 類型. normal | mohu | full | ymd | other * @param string $alt 已失效 * @return string */ function friendlyDate($sTime,$type = 'normal',$alt = 'false') { if (!$sTime) return ''; //sTime=源時間,cTime=當前時間,dTime=時間差 $cTime = time(); $dTime = $cTime - $sTime; $dDay = intval(date("z",$cTime)) - intval(date("z",$sTime)); //$dDay = intval($dTime/3600/24); $dYear = intval(date("Y",$cTime)) - intval(date("Y",$sTime)); //normal:n秒前,n分鐘前,n小時前,日期 if($type=='normal'){ if( $dTime < 60 ){ if($dTime < 10){ return '剛剛'; //by yangjs }else{ return intval(floor($dTime / 10) * 10)."秒前"; } }elseif( $dTime < 3600 ){ return intval($dTime/60)."分鐘前"; //今天的數據.年份相同.日期相同. }elseif( $dYear==0 && $dDay == 0 ){ //return intval($dTime/3600)."小時前"; return '今天'.date('H:i',$sTime); }elseif($dYear==0){ return date("m月d日 H:i",$sTime); }else{ return date("Y-m-d H:i",$sTime); } }elseif($type=='mohu'){ if( $dTime < 60 ){ return $dTime."秒前"; }elseif( $dTime < 3600 ){ return intval($dTime/60)."分鐘前"; }elseif( $dTime >= 3600 && $dDay == 0 ){ return intval($dTime/3600)."小時前"; }elseif( $dDay > 0 && $dDay<=7 ){ return intval($dDay)."天前"; }elseif( $dDay > 7 && $dDay <= 30 ){ return intval($dDay/7) . '周前'; }elseif( $dDay > 30 ){ return intval($dDay/30) . '個月前'; } //full: Y-m-d , H:i:s }elseif($type=='full'){ return date("Y-m-d , H:i:s",$sTime); }elseif($type=='ymd'){ return date("Y-m-d",$sTime); }else{ if( $dTime < 60 ){ return $dTime."秒前"; }elseif( $dTime < 3600 ){ return intval($dTime/60)."分鐘前"; }elseif( $dTime >= 3600 && $dDay == 0 ){ return intval($dTime/3600)."小時前"; }elseif($dYear==0){ return date("Y-m-d H:i:s",$sTime); }else{ return date("Y-m-d H:i:s",$sTime); } } }