Advertisement
Redfern_89

format_time funtion

May 26th, 2022
578
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     function format_time($secs) {
  3.         $result = array();
  4.        
  5.         if ($secs >= 31536000) {
  6.             $result[] = sprintf('%dy', floor($secs / 31536000));
  7.             $result[] = sprintf('%dd', floor($secs % 31536000 / 86400));
  8.             $result[] = sprintf('%dh', floor($secs % 86400 / 3600));
  9.             $result[] = sprintf('%dm', floor($secs % 86400 % 3600 / 60));
  10.             $result[] = sprintf('%ds', floor($secs % 86400 % 3600 % 60));
  11.         } else if ($secs >= 86400 && $secs < 31536000) {
  12.             $result[] = sprintf('%dd', floor($secs / 86400));
  13.             $result[] = sprintf('%dh', floor($secs % 86400 / 3600));
  14.             $result[] = sprintf('%dm', floor($secs / 60 % 60));
  15.             $result[] = sprintf('%ds', floor($secs % 60));
  16.         } else if ($secs >= 3600 && $secs < 86400) {
  17.             $result[] = sprintf('%dh', floor($secs / 3600));
  18.             $result[] = sprintf('%dm', floor($secs / 60 % 60));
  19.             $result[] = sprintf('%ds', floor($secs % 60));
  20.         } else if ($secs >= 60 && $secs < 3600) {
  21.             $result[] = sprintf('%dm', floor($secs / 60));
  22.             $result[] = sprintf('%ds', floor($secs % 60));
  23.         } else if ($secs < 60) {
  24.             $result[] = sprintf('%ds', $secs);
  25.         }
  26.        
  27.         return implode(' ', $result);      
  28.     }
  29.  
  30.     echo format_time(32317533);  // 1y 9d 1h 5m 33s
  31.  
  32. ?>
Advertisement
RAW Paste Data Copied
Advertisement