Home  >  Article  >  Backend Development  >  PHP convert seconds to days, hours and minutes

PHP convert seconds to days, hours and minutes

PHP中文网
PHP中文网Original
2017-03-20 15:09:177187browse

PHP converts seconds to days, hours and minutes:

function secsToStr($secs) {
    if($secs>=86400){$days=floor($secs/86400);
    $secs=$secs%86400;
    $r=$days.' day';
    if($days<>1){$r.=&#39;s&#39;;}
    if($secs>0){$r.=&#39;, &#39;;}}
    if($secs>=3600){$hours=floor($secs/3600);
    $secs=$secs%3600;
    $r.=$hours.&#39; hour&#39;;
    if($hours<>1){$r.=&#39;s&#39;;}
    if($secs>0){$r.=&#39;, &#39;;}}
    if($secs>=60){$minutes=floor($secs/60);
    $secs=$secs%60;
    $r.=$minutes.&#39; minute&#39;;
    if($minutes<>1){$r.=&#39;s&#39;;}
    if($secs>0){$r.=&#39;, &#39;;}}
    $r.=$secs.&#39; second&#39;;
    if($secs<>1){$r.=&#39;s&#39;;
    }
    return $r;
}

Usage:

<?php
$seconds = "56789";
$output = secsToStr($seconds);
echo $output;
?>

The above is the content of PHP to convert seconds to days, hours and minutes. For more related content, please pay attention to php Chinese website
Related tools: Unix timestamp conversion tool http://www.php.cn/xiazai/tool/5

Related articles:

Time conversion in js - sample code for converting milliseconds to date and time

PHP Convert seconds to days, hours and minutes format time

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