You are here

function weather_format_temperature in Weather 7

Same name and namespace in other branches
  1. 7.3 weather_theme.inc \weather_format_temperature()
  2. 7.2 weather_theme.inc \weather_format_temperature()

Converts temperatures.

Parameters

int $temperature: Temperature in degree celsius.

string $unit: Unit to be returned (celsius, fahrenheit, ...).

Return value

string Formatted representation in the desired unit.

2 calls to weather_format_temperature()
theme_weather_theming in ./weather_theme.inc
Custom theme function for preprocessing the weather display.
weather_format_apparent_temperature in ./weather_theme.inc
Calculates apparent temperature.

File

./weather_theme.inc, line 186
Prepare themed weather output.

Code

function weather_format_temperature($temperature, $unit) {

  // Do the calculation
  $fahrenheit = (int) ($temperature * 9 / 5) + 32;

  // Format the temperature
  if ($unit == 'fahrenheit') {
    $result = t('!temperature °F', array(
      '!temperature' => $fahrenheit,
    ));
  }
  elseif ($unit == 'celsiusfahrenheit') {
    $result = t('!temperature_c °C / !temperature_f °F', array(
      '!temperature_c' => $temperature,
      '!temperature_f' => $fahrenheit,
    ));
  }
  elseif ($unit == 'fahrenheitcelsius') {
    $result = t('!temperature_f °F / !temperature_c °C', array(
      '!temperature_f' => $fahrenheit,
      '!temperature_c' => $temperature,
    ));
  }
  else {

    // Default to metric units.
    $result = t('!temperature °C', array(
      '!temperature' => $temperature,
    ));
  }
  return preg_replace("/([^ ]*)&thinsp;([^ ]*)/", '<span style="white-space:nowrap;">\\1&thinsp;\\2</span>', $result);
}