时间格式化转换
时间格式化转换
/**
* 计算持续时长
*
* @param int $second 秒数
* @return string $duration 5天10小时43分钟40秒
*/
function secondTime($seconds=0){
$duration = '';
$seconds = (int) $seconds;
if ($seconds <= 0) {
return $duration.'0秒';
}
list($day, $hour, $minute, $second) = explode(' ', gmstrftime('%j %H %M %S', $seconds));
$day -= 1;
if ($day > 0) {
$duration .= (int) $day.'天';
}
if ($hour > 0) {
$duration .= (int) $hour.'小时';
}
if ($minute > 0) {
$duration .= (int) $minute.'分';
}
if ($second > 0) {
$duration .= (int) $second.'秒';
}
return $duration;
}
/**
* 文章时间友好化
* @param null $time 添加时间,时间戳
* @return string
*/
function friendlyDate($time = null) {
if (!$time) {
return '';
}
// 获取当前时间戳
$cTime = time();
$date = date('Y.m.d', $time);
$xyh = intval(($cTime - $time) / 24 / 3600);
// 获取当前时间戳与$time的差
$dTime = $cTime - $time;
if ($dTime < 60) {
return intval($dTime) . "秒前";
} elseif ($dTime < 3600) {
return intval($dTime / 60) . "分钟前";
} elseif ($dTime >= 3600 && $xyh < 1) {
return intval($dTime / 3600) . "小时前";
} elseif ($xyh > 365) {
// 如果时间大于1年,返回具体日期
return $date;
} elseif ($xyh == 1) {
return "昨天";
} elseif ($xyh >= 2 && $xyh <= 13) {
return intval($xyh) . "天前";
} elseif ($xyh > 13 && $xyh <= 60) {
return intval($xyh / 7) . "周前";
} elseif ($xyh > 60) {
return intval($xyh / 30) . "月前";
}
}
/**
* 朋友圈日期处理
* @return string
*/
function time_ago($time = 0, $nowTime = 0) {
if (!$nowTime) $nowTime = time();
$dif = $nowTime - $time;
if ($dif < 1)
return '刚刚';
if ($dif < 60)
return $dif . '秒前';
$dif = $dif / 60;
if (1 <= $dif && $dif < 60)
return (int)$dif . '分钟前';
$dif = $dif / 60;
if (1 <= $dif && $dif < 24)
return (int)$dif . '小时前';
//最多显示3天前
$dif = $dif / 24;
if (1 <= $dif && $dif <= 3)
return (int)$dif . '天前';
return date('m-d', $time);
}

浙公网安备 33010602011771号