网上常见的 刚刚发表,发表于一年前,,等等时间差是怎么实现的呢?一个简单的函数就可以解决
private function timeProcess($timestamp)
{
$now_time = time();
$time_gap = $now_time - $timestamp;
if ($time_gap < 60) {
return '刚刚 ' . date('H:i', $timestamp);
} elseif ($time_gap >= 60 && $time_gap < 60 * 60) {
return floor($time_gap / 60) . '分钟前 ' . date('H:i', $timestamp);
} elseif ($time_gap >= 60 * 60 && $time_gap < 60 * 60 * 60) {
return floor($time_gap / (60 * 60)) . '小时前 ' . date('H:i', $timestamp);
} elseif ($time_gap >= (60 * 60 * 24) && $time_gap < (60 * 60 * 24 * 7)) {
return floor($time_gap / (60 * 60 * 24)) . '天前 ' . date('H:i', $timestamp);
} elseif ($time_gap >= (60 * 60 * 24 * 7) && $time_gap < (60 * 60 * 24 * 30)) {
return floor($time_gap / (60 * 60 * 24 * 7)) . '周前 ' . date('H:i', $timestamp);
} elseif ($time_gap >= (60 * 60 * 24 * 30) && $time_gap < (60 * 60 * 24 * 30 * 12)) {
return floor($time_gap / (60 * 60 * 24 * 30)) . '个月前 ' . date('H:i', $timestamp);
} elseif ($time_gap >= (60 * 60 * 24 * 30 * 12)) {
return floor($time_gap / (60 * 60 * 24 * 30 * 12)) . '年前 ' . date('H:i', $timestamp);
}
}