function weather_get_forecasts_from_database in Weather 7.2
Same name and namespace in other branches
- 7.3 weather_parser.inc \weather_get_forecasts_from_database()
Try to fetch forecasts from the database.
Parameters
string $geoid: GeoID of the place for which the weather is desired.
string $utc_offset: UTC offset of place in minutes.
int $days: Return weather for specified number of days (0 = all available days).
bool $detailed: Return detailed forecasts or just one forecast per day.
int $time: Timestamp for which the weather should be returned. This is only needed to enable proper testing of the module.
Return value
array Weather array with forecast information.
1 call to weather_get_forecasts_from_database()
- weather_get_weather in ./
weather.common.inc - Returns a weather object for the specified GeoID.
File
- ./
weather_parser.inc, line 42 - Retrieves and parses raw METAR data and stores result in database.
Code
function weather_get_forecasts_from_database($geoid, $utc_offset, $days, $detailed, $time) {
// Fetch the first forecast. This must be done separately, because
// sometimes the first forecast is already on the next day (this can
// happen e.g. late in the evenings). Otherwise, the calculation of
// 'tomorrow' would fail.
$current_local_time = gmdate('Y-m-d H:i:s', $time + $utc_offset * 60);
$first_forecast = db_query('SELECT * FROM {weather_forecasts} WHERE geoid = :geoid AND time_to >= :current_local_time ORDER BY time_from ASC', array(
':geoid' => $geoid,
':current_local_time' => $current_local_time,
))
->fetchObject();
// If there are no forecasts available, return an empty array.
if ($first_forecast === FALSE) {
return array();
}
$weather = _weather_create_weather_array(array(
$first_forecast,
));
// Calculate tomorrow based on result.
$first_forecast_day = explode('-', key($weather));
$tomorrow_local_time = gmdate('Y-m-d H:i:s', gmmktime(0, 0, 0, $first_forecast_day[1], $first_forecast_day[2] + 1, $first_forecast_day[0]));
$forecasts_until_local_time = gmdate('Y-m-d 23:59:59', gmmktime(23, 59, 59, $first_forecast_day[1], $first_forecast_day[2] + $days - 1, $first_forecast_day[0]));
if ($detailed) {
// Fetch all available forecasts.
if ($days > 0) {
$forecasts = db_query('SELECT * FROM {weather_forecasts} WHERE geoid = :geoid AND time_to >= :current_local_time AND time_from <= :forecasts_until_local_time ORDER BY time_from ASC', array(
':geoid' => $geoid,
':current_local_time' => $current_local_time,
'forecasts_until_local_time' => $forecasts_until_local_time,
));
}
else {
$forecasts = db_query('SELECT * FROM {weather_forecasts} WHERE geoid = :geoid AND time_to >= :current_local_time ORDER BY time_from ASC', array(
':geoid' => $geoid,
':current_local_time' => $current_local_time,
));
}
$weather = _weather_create_weather_array($forecasts);
}
else {
if ($days > 1) {
$forecasts = db_query('SELECT * FROM {weather_forecasts} WHERE geoid = :geoid AND time_from >= :tomorrow_local_time AND period = \'2\' AND time_from <= :forecasts_until_local_time ORDER BY time_from ASC', array(
':geoid' => $geoid,
':tomorrow_local_time' => $tomorrow_local_time,
'forecasts_until_local_time' => $forecasts_until_local_time,
));
$weather = array_merge($weather, _weather_create_weather_array($forecasts));
}
elseif ($days == 0) {
$forecasts = db_query('SELECT * FROM {weather_forecasts} WHERE geoid = :geoid AND time_from >= :tomorrow_local_time AND period = \'2\' ORDER BY time_from ASC', array(
':geoid' => $geoid,
':tomorrow_local_time' => $tomorrow_local_time,
));
$weather = array_merge($weather, _weather_create_weather_array($forecasts));
}
}
return $weather;
}