Current Weather Conditions

The National Weather Service provides Current Weather Conditions XML files for many cities in the US.

Here is some code for PHP5 (it uses SimpleXML) on doing this (assuming you’ve already grabbed the XML file from their site and have a way to cache it). It even has some code calculating the sunrise, sunset and civil twilight which PHP5 can do with date_sun_info() and since the XML file gives the date, latitude and longitude, you can calculate it dynamically.

//PHP5
if (file_exists('KADS.xml'))
{
$xmlstr = file_get_contents('KADS.xml');
$xml = simplexml_load_string($xmlstr);
$datetime = strtotime($xml->observation_time_rfc822);
$latitude = (float)$xml->latitude;
$longitude = (float)$xml->longitude;
$sun_info = date_sun_info(date('Y-n-j', $datetime), $latitude, $longitude);//calculate sunrise,sunset,...
echo '<ul id="current_conditions">';
echo '<li>' . date('l, F j, Y', $datetime) . '</li>';
echo '<li class="weather_loc">' . $xml->location . '</li>';
echo '<!--' . $xml->observation_time . '-->';
echo '<li><img src="' . $xml->icon_url_base . $xml->icon_url_name . '" alt="' . $xml->weather . '" /></li>';
echo '<li class="weather_temp"><strong>' . $xml->temp_f . '&deg;' . '</strong></li>';
echo '<li class="weather_sum"><strong>' . $xml->weather . '</strong></li>';
echo '<li>Wind: <strong>' . $xml->wind_dir . ' ' . round($xml->wind_mph) . ' mph</strong></li>';
echo '<li>Humidity: <strong>' . $xml->relative_humidity . '%</strong></li>';
if ($xml->heat_index_f != 'NA')
echo '<li>Heat Index: <strong>' . $xml->heat_index_f . '</strong></li>';
if ($xml->windchill_f != 'NA')
echo '<li>Windchill: <strong>' . $xml->windchill_f . '</strong></li>';
echo '<li>Barometer: <strong>' . $xml->pressure_in . '&quot;</strong></li>';
echo '<li>Visibility: <strong>' . $xml->visibility_mi . ' mi</strong></li>';
echo '<li>Dewpoint: <strong>' . $xml->dewpoint_f . '&deg;</strong></li>';
echo '<li class="weather_credit">weather by <a href="' . $xml->credit_URL . '" title="' . $xml->credit . '">NOAA</a></li>';
echo '</ul>';
echo '<ul id="sunrise_sunset">';
echo '<li>Sunrise: <strong>' . date('g:i a', $sun_info['sunrise']) . '</strong> <span class="civil_twilight">Civil Twilight: <strong>' . date('g:i a', $sun_info['civil_twilight_begin']) . '</strong></span></li>';
echo '<li>Sunrise: <strong>' . date('g:i a', $sun_info['sunset']) . '</strong> <span class="civil_twilight">Civil Twilight: <strong>' . date('g:i a', $sun_info['civil_twilight_end']) . '</strong></span></li>';
echo '</ul>';
}

You will probably want to cache that output once you have it formatted how you like.

Tags: , , ,

One Comment to “Current Weather Conditions”

  1. -49 · Caio says:

    Angel, yeah you are right, not that many people are as nice as you and acllatuy gave tissue to a stranger. It is hard to be an a-hole to a nice person :-).I remember the SARS time..I wasn’t in HK, and as I was working in a lab and have tons of those N95 masks, my mom (who was in HK around that time) kept asking me to send her some because apparently it was hard to get in HK. This is okay if it is just one or two, but then her friends knew and I got tons of orders from her and I just had to say I am sorry but not anymore…(My mom was a bit annoyed, hahaha)

Leave a Comment

Comments are reviewed before publishing to prevent spam.