/** * 时间戳人性化转化 * @param $time * @return string */ function timeFormat($time) { $t = time() - $time; $f = array( '31536000' => '年', '2592000' => '个月', '604800' => '星期', '86400' => '天', '3600' => '小时', '60' => '分钟', '1' => '秒' ); foreach ($f as $k => $v) { if (0 != $c = floor($t / (int)$k)) { return $c . $v . '前'; } } }
测试:
print_r(time_tran(1669173460)); // 4分钟前 print_r(time_tran(1669153460)); // 5小时前 print_r(time_tran(1668153460)); // 1星期前 print_r(time_tran(1658153460)); // 4个月前 print_r(time_tran(1628153460)); // 1年前