分享一个时间戳和当前时间差值处理的函数,用于返回‘刚刚’,‘X分钟前’,‘x小时前’……
if (!function_exists('getDateDiff')) {
/**
* PHP 时间戳差值文字处理
* @param [type] $dateTimeStamp [ 待处理数据时间戳 ]
* @return [type] [ 返回时间差值文字 ]
*
* demo
* echo getDateDiff(time()-3600);
*/
function getDateDiff($dateTimeStamp) {
$minute = 60;
$hour = $minute * 60;
$day = $hour * 24;
$halfamonth = $day * 15;
$month = $day * 30;
$now = time();
// 计算时间差
$diffValue = $now - $dateTimeStamp;
if ($diffValue < 0) {
return;
}
$monthC = $diffValue / $month;
$weekC = $diffValue / (7 * $day);
$dayC = $diffValue / $day;
$hourC = $diffValue / $hour;
$minC = $diffValue / $minute;
$result = '';
if ($monthC >= 1) {
$result = intval($monthC)."月前";
} else if ($weekC >= 1) {
$result = intval($weekC)."周前";
} else if ($dayC >= 1) {
$result = intval($dayC)."天前";
} else if ($hourC >= 1) {
$result = intval($hourC)."小时前";
} else if ($minC >= 1) {
$result = intval($minC)."分钟前";
} else {
$result = "刚刚";
}
return $result;
}
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/266784.html