将显示日期YY-MM-DD H:i:s改为相对时间展示更为直观,比如说“几分钟之前”,“几小时之前”,“刚刚”,下面是相对时间函数。
//获取相对时间,传参可以是日期 如 "2015-6-16 12:10:10"; ,也可以是时间戳
function getDiffTime($time) {
if($time == 0 || $time == ""){
return "";
}
if(is_numeric($time)){
$diff = time() - $time;
}else{
$diff = time() - strtotime($time);
}
if($diff > 0 ){
if ($diff<60)
return sprintf(L('DIFF_TIME_MINUTE_AGO'),$diff);
$diff = round($diff/60);
if ($diff<60)
return sprintf(L('DIFF_TIME_SECOND_AGO'),$diff);
$diff = round($diff/60);
if ($diff<24)
return sprintf(L('DIFF_TIME_HOUR_AGO'),$diff);
$diff = round($diff/24);
if ($diff<7)
return sprintf(L('DIFF_TIME_DAY_AGO'),$diff);
$diff = round($diff/7);
if ($diff<4)
return sprintf(L('DIFF_TIME_WEEK_AGO'),$diff);
}else{
$diff = abs($diff);
if ($diff<60)
return sprintf(L('DIFF_TIME_MINUTE_AFTER'),$diff);
$diff = round($diff/60);
if ($diff<60)
return sprintf(L('DIFF_TIME_SECOND_AFTER'),$diff);
$diff = round($diff/60);
if ($diff<24)
return sprintf(L('DIFF_TIME_HOUR_AFTER'),$diff);
$diff = round($diff/24);
if ($diff<7)
return sprintf(L('DIFF_TIME_DAY_AFTER'),$diff);
$diff = round($diff/7);
if ($diff<4)
return sprintf(L('DIFF_TIME_WEEK_AFTER'),$diff);
}
if(is_numeric($time)){
return date('Y-m-d',$time);
}else{
return date("Y-m-d", strtotime($time));
}
}