You are here

protected function field_timer_text_base::formatInterval in Field Timer 7.2

Formats interval according to granularity settings.

1 call to field_timer_text_base::formatInterval()
field_timer_text_base::renderItem in includes/field_timer_text.inc
Render field item.

File

includes/field_timer_text.inc, line 153
Contains help classes to perform field_timer_text formatter related actions.

Class

field_timer_text_base
Base class for field_timer_text formatter.

Code

protected function formatInterval($interval, $granularity_type, $granularity_option) {
  $result = '';
  switch ($granularity_type) {
    case 'auto':
      $result = format_interval($interval, $granularity_option);
      break;
    case 'manual':

      // @see function format_interval
      $units = array(
        '1 year|@count years' => 31536000,
        '1 month|@count months' => 2592000,
        '1 week|@count weeks' => 604800,
        '1 day|@count days' => 86400,
        '1 hour|@count hours' => 3600,
        '1 min|@count min' => 60,
        '1 sec|@count sec' => 1,
      );
      $granularity_options = array_keys($this
        ->formatterGranularityManual());
      $output = '';
      foreach ($units as $key => $value) {
        if (isset($granularity_option[array_pop($granularity_options)])) {
          $key = explode('|', $key);
          if ($interval >= $value) {
            $output .= ($output ? ' ' : '') . format_plural(floor($interval / $value), $key[0], $key[1]);
            $interval %= $value;
          }
        }
      }
      $result = $output ? $output : (isset($key) ? format_plural(0, $key[0], $key[1]) : format_interval($interval));
      break;
  }
  return $result;
}