Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 0.95 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Given a decimal time value, how could I round that to an interval?
  2. function round_decimal_time($time, $interval=15){
  3.   // Split up decimal time
  4.   $hours = (int) $time;
  5.   $minutes = $time - $hours;
  6.  
  7.   // Convert base 10 minutes to base 60 minutes
  8.   $b60_m = $minutes * 60;
  9.  
  10.   // Round base 60 minutes to nearest interval... (15 minutes by default)
  11.   // DONT KNOW HOW TO DO THIS PART
  12.  
  13.   // If greater than or equal to 60, go up an hour
  14.   if($b60_m >= 60){
  15.       $hours += 1;
  16.       $minutes = 0;
  17.   } else {
  18.     // Otherwise, convert b60 minutes back into b10
  19.     $time = $hours + ($b60_m / 60);
  20.   }
  21.  
  22.   return $time;
  23. }
  24.        
  25. Input: 5.66 (5:39 duration)
  26. Output: 5.75
  27.  
  28. Input: 5.6 (5:36 duration)
  29. Output: 5.50
  30.  
  31. Input: 5.05 (5:03 duration)
  32. Output: 5.00
  33.        
  34. round($number/$X)*$X;
  35.        
  36. round(39.6/15)*15=45
  37.        
  38. round_decimal_time($time,$round=15){
  39.      return (round($time * 60 / $round) * $round) / 60;
  40.  }
  41.        
  42. $a = 5.66;
  43. var_dump(round($a / 0.25) * 0.25);
  44.        
  45. round(7 / 5) * 5