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

PHP method based on seconds PHP duration

不言
Release: 2023-04-02 11:48:01
Original
2668 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template