發帖時,顯示幾秒前發表,幾分鐘前發表,幾小時前發表php
親測可用,項目中用到了spa
function gettime($time,$time_n){ $strtime = ''; $time = $time-$time_n; if($time >= 86400){ $strtime .= date("Y-m-d", $time_n); return $strtime; } if($time >= 3600){ $strtime .= intval($time/3600).'小時'; return $strtime; } if($time >= 60){ $strtime .= intval($time/60).'分鐘'; return $strtime; } if($time >= 0){ $strtime .= intval($time).'秒'; return $strtime; }else{ $strtime = "時間錯誤"; } } $time1 = time(); $time2 = 1407913674; $str = gettime($time1,$time2); var_dump($str);
另一種格式:code
<?php function gettime($time,$time_n){ $strtime = ''; $time = $time_n-$time; if($time >= 86400){ $strtime .= intval($time/86400).'天'; $time = $time % 86400; }else{ $strtime .= ''; } if($time >= 3600){ $strtime .= intval($time/3600).'小時'; $time = $time % 3600; }else{ $strtime .= ''; } if($time >= 60){ $strtime .= intval($time/60).'分鐘'; $time = $time % 60; }else{ $strtime .= ''; } if($time >= 0){ $strtime .= intval($time).'秒'; }else{ $strtime = "時間錯誤"; } return $strtime; } $time1 = time(); $time2 = 1411517340; $str = gettime($time1,$time2); var_dump($str);