php中时间日期转换

本文章介绍了一篇php中时间日期转换自定函数,它可以实现将秒数表示的事件转换为年、月、日、小时等时间格式。

  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. }