代码提交
This commit is contained in:
@@ -488,6 +488,38 @@ if (!function_exists('getUserAction')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('formatRelativeTime')) {
|
||||
/**
|
||||
* 将时间戳格式化为相对时间(中文)
|
||||
* 例:半年前 / 1个月前 / 3周前 / 1天前 / 5小时前 / 5分钟前 / 刚刚
|
||||
* @param int $timestamp Unix 时间戳(秒)
|
||||
* @return string
|
||||
*/
|
||||
function formatRelativeTime($timestamp)
|
||||
{
|
||||
if (empty($timestamp) || !is_numeric($timestamp)) {
|
||||
return '';
|
||||
}
|
||||
$now = time();
|
||||
$diff = max(0, $now - (int)$timestamp);
|
||||
|
||||
$minute = 60;
|
||||
$hour = 60 * $minute;
|
||||
$day = 24 * $hour;
|
||||
$week = 7 * $day;
|
||||
$month = 30 * $day; // 近似
|
||||
$halfYear = 6 * $month; // 近似
|
||||
|
||||
if ($diff >= $halfYear) return '半年前';
|
||||
if ($diff >= $month) return floor($diff / $month) . '个月前';
|
||||
if ($diff >= $week) return floor($diff / $week) . '周前';
|
||||
if ($diff >= $day) return floor($diff / $day) . '天前';
|
||||
if ($diff >= $hour) return floor($diff / $hour) . '小时前';
|
||||
if ($diff >= $minute) return floor($diff / $minute) . '分钟前';
|
||||
return '刚刚';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('exit_data')) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user