Home > Backend Development > PHP Tutorial > PHP convert seconds to time (year, month, day, hour...)

PHP convert seconds to time (year, month, day, hour...)

WBOY
Release: 2016-07-25 08:42:54
Original
1972 people have browsed it

This useful function can convert events represented by seconds into time formats such as year, month, day, hour, etc.

  1. function Sec2Time($time){
  2. if(is_numeric($time)){
  3. $value = array(
  4. "years" => 0, "days" => 0, "hours" => 0,
  5. "minutes" => 0, "seconds" => 0,
  6. );
  7. if( $time >= 31556926){
  8. $value["years"] = floor($time/31556926);
  9. $time = ($time%31556926);
  10. }
  11. if($time >= 86400){
  12. $value["days"] = floor($time/86400);
  13. $time = ($time%86400);
  14. }
  15. if($time >= 3600){
  16. $value["hours"] = floor ($time/3600);
  17. $time = ($time%3600);
  18. }
  19. if($time >= 60){
  20. $value["minutes"] = floor($time/60);
  21. $ time = ($time%60);
  22. }
  23. $value["seconds"] = floor($time);
  24. return (array) $value;
  25. }else{
  26. return (bool) FALSE;
  27. }
  28. }
Copy code

Convert to, PHP, seconds


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