public static function formatTime($time)
{
if (is_int($time)) {
$time = intval($time);
} elseif ($time instanceof Carbon) {
$time = intval(strtotime($time));
} else {
return '';
}
$ctime = time();
$t = $ctime - $time; //时间差 (秒)
if ($t < 0) {
return date('Y-m-d', $time);
}
$y = intval(date('Y', $ctime) - date('Y', $time));//是否跨年
/**/
$today=strtotime(date('Y-m-d',strtotime('0 day')));
if($time > $today) {//一天内
$text = '今天'.date(" H:i",$time); // 一天内
} elseif ($t < 2592000) {//30天内
if ($time > strtotime(date('Ymd', strtotime("-1 day")))) {
$text = '昨天'.date(" H:i",$time);
} elseif ($time > strtotime(date('Ymd', strtotime("-2 days")))) {
$text = '前天'.date(" H:i",$time);
} else {
$text = floor($t / 86400) . '天前';
}
} elseif ($t < 31536000 && $y == 0) {//一年内 不跨年
$m = date('m', $ctime) - date('m', $time) - 1;
if ($m == 0) {
$text = floor($t / 86400) . '天前';
} else {
$text = $m . '个月前';
}
} elseif ($t < 31536000 && $y > 0) {//一年内 跨年
$text = (12 - date('m', $time) + date('m', $ctime)) . '个月前';
} else {
$text = (date('Y', $ctime) - date('Y', $time)) . '年前';
}
return $text;
}