function formatTime($time) {
if ($time < 60) {
$format = '刚刚';
} elseif ($time >= 60 && $time < 3600) {
$format = intval($time / 60) . '分钟前';
} elseif ($time >= 3600 && $time < 86400) {
$format = intval($time / 3600) . '小时前';
} elseif ($time >= 86400 && $time < 86400 * 2) {
$format = '昨天';
} elseif ($time >= 86400 * 2 && $time < 86400 * 30) {
$format = intval($time / 86400) . '天前';
} elseif ($time >= 86400 * 30 && $time < 86400 * 365) {
$format = intval($time / 86400 / 30) . '月前';;
} else {
$format = '一年前';
}
return $format;
}
function formatTime2($time) {
$time = strtotime('2016-11-24');
$datetime = new DateTime(date('Y-m-d H:i:s', $time));
$datetime_now = new DateTime();
$interval = $datetime_now->diff($datetime);
list($y, $m, $d, $h, $i, $s) = explode('-', $interval->format('%y-%m-%d-%h-%i-%s'));
if (
(($result = $y) && ($suffix = '年前')) ||
(($result = $m) && ($suffix = '月前')) ||
(($result = $d) && ($suffix = '天前')) ||
(($result = $h) && ($suffix = '小时前')) ||
(($result = $i) && ($suffix = '分钟前')) ||
(($result = $s) && ($suffix = '刚刚'))
) {
$suffix = $suffix != '刚刚' ? $result . $suffix : $suffix;
}
}
if ($time < 60) {
$format = '刚刚';
} elseif ($time >= 60 && $time < 3600) {
$format = intval($time / 60) . '分钟前';
} elseif ($time >= 3600 && $time < 86400) {
$format = intval($time / 3600) . '小时前';
} elseif ($time >= 86400 && $time < 86400 * 2) {
$format = '昨天';
} elseif ($time >= 86400 * 2 && $time < 86400 * 30) {
$format = intval($time / 86400) . '天前';
} elseif ($time >= 86400 * 30 && $time < 86400 * 365) {
$format = intval($time / 86400 / 30) . '月前';;
} else {
$format = '一年前';
}
return $format;
}
function formatTime2($time) {
$time = strtotime('2016-11-24');
$datetime = new DateTime(date('Y-m-d H:i:s', $time));
$datetime_now = new DateTime();
$interval = $datetime_now->diff($datetime);
list($y, $m, $d, $h, $i, $s) = explode('-', $interval->format('%y-%m-%d-%h-%i-%s'));
if (
(($result = $y) && ($suffix = '年前')) ||
(($result = $m) && ($suffix = '月前')) ||
(($result = $d) && ($suffix = '天前')) ||
(($result = $h) && ($suffix = '小时前')) ||
(($result = $i) && ($suffix = '分钟前')) ||
(($result = $s) && ($suffix = '刚刚'))
) {
$suffix = $suffix != '刚刚' ? $result . $suffix : $suffix;
}
}
来源:原创
发布时间:2020-09-09 21:29:07