建站之友好时间显示
public function format($time)
{
if (invalid_integer($time)) {
return '';
}
$now = time();
$today = strtotime(date('Y-m-d', $now));
$tmp = $now - $time;
if (60 > $tmp) {
return '刚刚';
} elseif (3600 > $tmp) {
return floor($tmp / 60) . ' 分钟前';
} elseif (86400 > $tmp) {
return floor($tmp / 3600) . ' 小时前';
}
$time = strtotime(date('Y-m-d', $time));
foreach (['昨', '前'] as $k => $name) {
if ($today - 86400 * (1 + $k) <= $time) {
return $name . '天';
}
}
$week = ['日', '一', '二', '三', '四', '五', '六'];
$tmp = date('w', $now);
0 == $tmp && $tmp = 7;
$tmp = $today - 86400 * $tmp;
foreach (['', '上'] as $k => $name) {
if ($tmp - 86400 * 7 * $k < $time) {
return $name . '周' . $week[date('w', $time)];
}
}
if ($tmp - 86400 * 7 * 2 < $time) {
return '3 周前';
}
$tmp = strtotime(date('Y-m-01', $now));
if ($tmp <= $time) {
return format_integer(($today - $time) / 86400) . ' 天前';
}
$tmp = strtotime(date('Y-m-01', $tmp - 1));
if ($tmp <= $time) {
return '上月';
}
$tmp = strtotime(date('Y-01-01', $now));
if ($tmp <= $time) {
return (date('n', $now) - date('n', $time)) . ' 月前';
}
foreach (['去', '前'] as $name) {
$tmp = strtotime(date('Y-01-01', $tmp - 1));
if ($tmp <= $time) {
return $name . '年';
}
}
return (date('Y', $now) - date('Y', $time)) . ' 年前';
}
标签: 建站