Home > Backend Development > PHP Tutorial > php convert seconds to time (year, day, hour, minute, second)

php convert seconds to time (year, day, hour, minute, second)

WBOY
Release: 2016-07-29 09:13:47
Original
2532 people have browsed it

$t=1637544;

$d=Sec2Time($t);

$d is 0 years, 18 days, 22 hours, 52 minutes and 24 seconds

//Convert seconds to time (year, day, hour , minutes, seconds)

php 将秒数转换为时间(年、天、小时、分、秒)
<span>function</span> Sec2Time(<span>$time</span><span>){
    </span><span>if</span>(<span>is_numeric</span>(<span>$time</span><span>)){
    </span><span>$value</span> = <span>array</span><span>(
      </span>"years" => 0, "days" => 0, "hours" => 0,
      "minutes" => 0, "seconds" => 0,<span>
    );
    </span><span>if</span>(<span>$time</span> >= 31556926<span>){
      </span><span>$value</span>["years"] = <span>floor</span>(<span>$time</span>/31556926<span>);
      </span><span>$time</span> = (<span>$time</span>%31556926<span>);
    }
    </span><span>if</span>(<span>$time</span> >= 86400<span>){
      </span><span>$value</span>["days"] = <span>floor</span>(<span>$time</span>/86400<span>);
      </span><span>$time</span> = (<span>$time</span>%86400<span>);
    }
    </span><span>if</span>(<span>$time</span> >= 3600<span>){
      </span><span>$value</span>["hours"] = <span>floor</span>(<span>$time</span>/3600<span>);
      </span><span>$time</span> = (<span>$time</span>%3600<span>);
    }
    </span><span>if</span>(<span>$time</span> >= 60<span>){
      </span><span>$value</span>["minutes"] = <span>floor</span>(<span>$time</span>/60<span>);
      </span><span>$time</span> = (<span>$time</span>%60<span>);
    }
    </span><span>$value</span>["seconds"] = <span>floor</span>(<span>$time</span><span>);
    </span><span>//</span><span>return (array) $value;</span><span>$t</span>=<span>$value</span>["years"] ."年". <span>$value</span>["days"] ."天"." ". <span>$value</span>["hours"] ."小时". <span>$value</span>["minutes"] ."分".<span>$value</span>["seconds"]."秒"<span>;
    </span><span>Return</span><span>$t</span><span>;
    
     }</span><span>else</span><span>{
    </span><span>return</span> (bool) <span>FALSE</span><span>;
    }
 }</span>
Copy after login

The above introduces PHP to convert seconds into time (years, days, hours, minutes, seconds), including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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