ght.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. function ght($ts,$fa=null,$sd=2) {
  3. /*
  4. $ts is seconds (can be float)
  5. if not null, $fa has to be an array defining the output suffixes, see below
  6. its default ;-)
  7. $sd is how many decimals to put after a dot after seconds (can be 0)
  8. */
  9. if ($fa==null)
  10. $fa=[' year, ',' years, ',' week, ',' weeks, ',' day, ',' days, ',' hour, ',' hours, ',' minute, ',' minutes, ',' second',' seconds'];
  11. $out='';
  12. $i=0;
  13. // years
  14. $x=floor($ts/31536000);
  15. if ($x>0)
  16. ($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
  17. $ts=$ts-$x*31536000;
  18. $i+=2;
  19. // weeks
  20. $x=floor($ts/604800);
  21. if ($x>0)
  22. ($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
  23. $ts=$ts-$x*604800;
  24. $i+=2;
  25. // days
  26. $x=floor($ts/86400);
  27. if ($x>0)
  28. ($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
  29. $ts=$ts-$x*86400;
  30. $i+=2;
  31. // hours
  32. $x=floor($ts/3600);
  33. if ($x>0)
  34. ($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
  35. $ts=$ts-$x*3600;
  36. $i+=2;
  37. // minutes
  38. $x=floor($ts/60);
  39. if ($x>0)
  40. ($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
  41. $ts=$ts-$x*60;
  42. $i+=2;
  43. // seconds
  44. $x=round($ts,$sd);
  45. ($x==1) ? $out.=$x.$fa[$i] : $out.=$x.$fa[$i+1];
  46. return $out;
  47. }
  48. ?>