function one:
/**
* Notes: 根据传送过来的时间戳判断时间是多久前
* @param $the_time 时间戳 (亦可为日期格式,转换好就行)
*/
function timeTran($time)
{
$nowTime = time();
$showTime = strtotime($time);
$difference = $nowTime - $showTime;
if ($difference < 0) {
return $time;
}
if ($difference < 60) {
return $difference . '秒前';
}
if ($difference < 3600) {
return floor($difference / 60).'分钟前';
}
if ($difference < 86400) {
return floor($difference / 3600).'小时前';
}
if ($difference < 2592000) {
return floor($difference / 86400) . '天前'; //30天内
}
if ($difference < 31104000) {
return floor($difference / 2592000) . '个月前'; //12个月内
}
return floor($difference / 31536000) . '年前';
}
function two:
/**
* Notes: 根据传送过来的时间戳判断 时间是 多久前
* @param $time 时间戳(亦可为日期格式,转换好就行)
*/
public function tranTime($time)
{
$rtime = date("m-d H:i", $time);
$htime = date("H:i", $time);
$time = time() - $time;
if ($time < 60) {
$str = '刚刚';
} elseif ($time < 60 * 60) {
$min = floor($time / 60);
$str = $min . '分钟前';
} elseif ($time < 60 * 60 * 24) {
$h = floor($time / (60 * 60));
$str = $h . '小时前 ' . $htime;
} elseif ($time < 60 * 60 * 24 * 3) {
$d = floor($time / (60 * 60 * 24));
if ($d == 1)
$str = '昨天 ' . $rtime;
else
$str = '前天 ' . $rtime;
} else {
$str = $rtime;
}
return $str;
}