Advertisement
dumle29

minutes splitter

Jan 15th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. /*---------------------------------------------------
  2. v0.2
  3. This function takes minutes as an input, and returns a
  4. readable string, fully formatted with plurals of words
  5. that need it. It should be easy to expand this to take
  6. seconds as input. If you need help, contact me on my
  7. website.
  8.  
  9. Made by Mikkel Jeppesen from http://snuletek.org
  10. Please don't remove the above line, thanks and enjoy :)
  11. ---------------------------------------------------*/
  12.  
  13. function minutesSplitter($minutes)
  14. {
  15.     $daySingular = 'day';
  16.     $dayPlural = 'days';
  17.    
  18.     $hourSingular = 'hour';    //Allows you to change the word used for the singularium of hour.
  19.     $hourPlural = 'hours';    //Allows you to change the word used for the plural of hour.
  20.    
  21.     $minutePlural = 'minutes';    //Allows you to change the word used for the singularium of minute.
  22.     $minuteSingular = 'minute';    //Allows you to change the word used for the plural of minute.
  23.    
  24.     $andStr = 'and';    //Allows you to change the word used to combine the hours and minutes string.
  25.    
  26.     $days = 0;
  27.     $hours = 0;
  28.    
  29.     if($minutes > 59)
  30.     {
  31.         $hours = floor($minutes / 60);
  32.         $minutes = $minutes % 60;
  33.     }
  34.     if($hours > 23)
  35.     {
  36.         $days = floor($hours / 24);
  37.         $hours = $hours % 24;  
  38.     }
  39.    
  40.     if($days > 1)
  41.     {
  42.         $dayStr = $days.' '.$dayPlural;
  43.     }
  44.     else if($days = 1)
  45.     {
  46.         $dayStr = $days.' '.$daySingular;
  47.     }
  48.     else
  49.     {
  50.         $dayStr = '';  
  51.     }
  52.    
  53.     if($hours > 1)
  54.     {
  55.         $hourStr = $hours.' '.$hourPlural;
  56.     }
  57.     else if($hours = 1)
  58.     {
  59.         $hourStr = $hours.' '.$hourSingular;
  60.     }
  61.     else
  62.     {
  63.         $hourStr = ''; 
  64.     }
  65.    
  66.     if($minutes > 1)
  67.     {
  68.         $minuteStr = $minutes. ' ' . $minutePlural;
  69.     }
  70.     else if($minutes = 1)
  71.     {
  72.         $minuteStr =$minutes. ' ' . $minuteSingular;
  73.     }
  74.     else if($minutes == 0 && !$hours)
  75.     {
  76.         $minuteStr =$minutes. ' ' . $minutePlural;
  77.     }
  78.     else
  79.     {
  80.         $minuteStr = '';   
  81.     }
  82.    
  83.     if($dayStr != '' && $hourStr != '')
  84.     {
  85.         $daysHoursComma = ', ';
  86.     }
  87.     else
  88.     {
  89.         $daysHoursComma = '';  
  90.     }
  91.         if($hourStr != '' && $minutes != '')
  92.     {
  93.         $andStr =' '. $andStr .' ';
  94.     }
  95.     else
  96.     {
  97.         $andStr = '';  
  98.     }
  99.    
  100.     return $dayStr . $daysHoursComma . $hourStr . $andStr . $minuteStr;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement