PHP根据秒计算持续时长

时间:2024-07-25 16:33:44
/**
* 计算持续时长
*
* @param int $second 秒数
* @return string $duration 5天10小时43分钟40秒
*/
function second2duration($seconds)
{
$duration = ''; $seconds = (int) $seconds;
if ($seconds <= 0) {
return $duration;
} 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;
}