<?php
function get_metar($station, &$wxInfo) {
$fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$station.TXT";
$metar = '';
$fileData = @file($fileName); // or die('Data not available');
if ($fileData != false) {
list($i, $date) = each($fileData);
$utc = strtotime(trim($date));
set_time_data($utc,$wxInfo);
while (list($i, $line) = each($fileData)) {
$metar .= ' ' . trim($line);
}
$metar = trim(str_replace(' ', ' ', $metar));
}
return $metar;
}
function set_time_data($utc, &$wxInfo) {
$timeZoneOffset = date('Z');
$local = $utc + $timeZoneOffset;
$wxInfo['OBSERVED'] = date('D M j, H:i T',$local);
$now = time();
$wxInfo['NOW'] = date('D M j, H:i T',$now);
$timeDiff = floor(($now - $local) / 60);
if ($timeDiff < 91) $wxInfo['AGE'] = "$timeDiff min ago";
else {
$min = $timeDiff % 60;
if ($min < 10) $min = '0' . $min;
$wxInfo['AGE'] = floor($timeDiff / 60) . ":$min hr ago";
}
}
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Expires: Sat,1 Jan 2000 00:00:01 GMT');
$station = $_GET['id'];
$station = strtoupper($station);
$wxInfo['STATION'] = $station;
$metar = get_metar($station,$wxInfo);
echo "$metar";
?>