You are here

function weather_format_temperature in Weather 7.2

Same name and namespace in other branches
  1. 7.3 weather_theme.inc \weather_format_temperature()
  2. 7 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.

3 calls to weather_format_temperature()
theme_weather_forecast_preprocess in ./weather_theme.inc
Custom theme function for preprocessing the weather display.
weather_format_windchill_temperature in ./weather_theme.inc
Calculates windchill temperature.
weather_handler_temperature::render in views_handlers/weather_handler_temperature.inc
Render temperature with selected unit.

File

./weather_theme.inc, line 205
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,
    ));
  }
  elseif ($unit == 'celsius_value') {
    $result = $temperature;
  }
  elseif ($unit == 'fahrenheit_value') {
    $result = $fahrenheit;
  }
  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);
}