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

PHP method based on seconds PHP duration

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

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(&#39; &#39;, gmstrftime(&#39;%j %H %M %S&#39;, $seconds));    
    $day -= 1;    
    if ($day > 0) {        
    $duration .= (int) $day.&#39;天&#39;;
    }    
    if ($hour > 0) {        
    $duration .= (int) $hour.&#39;小时&#39;;
    }    
    if ($minute > 0) {        
    $duration .= (int) $minute.&#39;分钟&#39;;
    }    
    if ($second > 0) {        
    $duration .= (int) $second.&#39;秒&#39;;
    }    
    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