You are here

function _weather_calculate_sun_info in Weather 7.3

Same name and namespace in other branches
  1. 7.2 weather_theme.inc \_weather_calculate_sun_info()

Calculate the times of sunrise and sunset.

Parameters

string $date: The date for which the calculation should be made.

int $utc_offset: UTC offset of local time in minutes.

string $geoid: The GeoID of a place.

Return value

array or string An array with sunrise and sunset times in the local timezone. If a string is returned, this is the special case for polar day or polar night without sunrise and sunset.

1 call to _weather_calculate_sun_info()
theme_weather_forecast_preprocess in ./weather_theme.inc
Custom theme function for preprocessing the weather display.

File

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

Code

function _weather_calculate_sun_info($date, $utc_offset, $geoid) {
  module_load_include('inc', 'weather', 'weather.common');

  // Get the coordinates for sunrise and sunset calculation.
  $coordinates = weather_get_information_about_geoid($geoid);

  // Calculate the timestamp for the local time zone.
  $time = strtotime($date . ' 12:00:00 UTC') - 60 * $utc_offset;
  $sun_info = date_sun_info($time, $coordinates->latitude, $coordinates->longitude);

  // Handle special cases (no sunrise or sunset at all).
  if ($sun_info['sunrise'] == 0 and $sun_info['sunset'] == 0) {

    // Sun is always below the horizon.
    $result = t('No sunrise, polar night');
  }
  elseif ($sun_info['sunrise'] == 1 and $sun_info['sunset'] == 1) {

    // Sun is always above the horizon.
    $result = t('No sunset, polar day');
  }
  else {

    // There is a sunrise and a sunset.
    // We don't need the exact second of the sunrise and sunset.
    // Therefore, the times are rounded to the next minute.
    $time = round($sun_info['sunrise'] / 60) * 60;
    $time = gmdate('H:i', $time + 60 * $utc_offset);
    $result['sunrise'] = $time;
    $time = round($sun_info['sunset'] / 60) * 60;
    $time = gmdate('H:i', $time + 60 * $utc_offset);
    $result['sunset'] = $time;
  }
  return $result;
}