Home  >  Article  >  Backend Development  >  PHP method based on seconds PHP duration

PHP method based on seconds PHP duration

不言
不言Original
2018-07-04 16:36:582456browse

This article mainly introduces the method of calculating duration based on seconds in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it

/**
 * 计算持续时长
 *
 * @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;
}

The above is the entire content of this article , I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

php method to calculate the relative path of two files

The above is the detailed content of PHP method based on seconds PHP duration. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn