Where pilots and airlines come together

The virtual airline blog

This is our blog with lots of useful tips about PHPvms and running VA's in general. We've just started but we got a lot in the pipeline, so keep an eye on this space!
Note that not all blog posts are public, so if you're not a member, sign up now to get the best out of this site. If you have any questions, don't hesitate to contact us.

Weather and sunrise/sunset

When flying, the weather makes quite a difference to the experience, and so does flying at night. Hence it's not a bad idea to display weather information, along with the time of sunset and sunrise, in your schedule details and flight briefing. It provides a lot of value to your pilots when it comes to choosing where to fly and in preparing their flight. It's also fairly easy thanks to the code I'm going to show you.

Let's start by looking at the two files where this information is useful:

1) Schedule_details: this is the page you see when you click on 'details' next to a scheduled flight
2) Schedule_briefing: this is the pilot briefing

The code is pretty much the same for both pages. Let's begin with sunrise/sunset:

$depsun="
Sunrise: ".date_sunrise(time(),SUNFUNCS_RET_STRING,$schedule->deplat,$schedule->deplng,90,0)." Sunset: ".date_sunset(time(),SUNFUNCS_RET_STRING,$schedule->deplat,$schedule->deplng,90,0)." (UTC)";

$arrsun="
Sunrise: ".date_sunrise(time(),SUNFUNCS_RET_STRING,$schedule->arrlat,$schedule->arrlng,90,0)." Sunset: ".date_sunset(time(),SUNFUNCS_RET_STRING,$schedule->arrlat,$schedule->arrlng,90,0)." (UTC)";

It uses the php builtin functions date_sunrise and date_sunset for the current day, and the latitude/longitude from the $schedule variable. All you have to do is echo $depsun; for sunset/sunrise times at the departure, and echo $arrsun; for the same at the destination. 
 
Right, let's crack on to the weather!
 
There's quite a bit of code involved, so it's best to create a separate functon in Schedulesdata.class.php in your /core/common folder. Do make a backup of the original file if you're not feeling proficient in php, as this has the potential to cause havoc across your entire site. 
 
At the end of the file, before the last } paste the following code:
 
function GetWeather($airport)
{
$url = "http://tgftp.nws.noaa.gov/data/observations/metar/decoded/".$airport.".TXT";
if ( function_exists( 'curl_init' ) ){
  $ch = curl_init();
 
  //curl_setopt ($ch,CURLOPT_POST, true);
  curl_setopt ($ch,CURLOPT_URL, $url );
curl_setopt ($ch,CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT, 5 );
 
$content = curl_exec( $ch );
curl_close( $ch );
}
else
{
  die( 'No method to read from remote server was found.' );
}
 
$content=str_replace("Wind","Wind",$content);
$content=str_replace("Visibility","Visibility",$content);
$content=str_replace("Sky conditions","Sky conditions",$content);
$content=str_replace("ob:","ob:",$content);
 
return $content;
}
 
Save the file and upload it to your host. Then go back to the schedule_details or schedule_briefing, and paste the following code:
 
$depweather=Schedulesdata::GetWeather($schedule->depicao);
$arrweather=Schedulesdata::GetWeather($schedule->arricao);
 
Bonus: add weather forecast
 
As a bonus, I got another little snippet which adds in the TAF (terminal area forecast) for the destination. Open up your SchedulesData again, and paste following code after the code you pasted earlier:
 
function GetForecast($airport)
{
$url="http://tgftp.nws.noaa.gov/data/forecasts/taf/stations/".$airport.".TXT";
if ( function_exists( 'curl_init' ) ){
  $ch = curl_init();
 
  //curl_setopt ($ch,CURLOPT_POST, true);
  curl_setopt ($ch,CURLOPT_URL, $url );
curl_setopt ($ch,CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT, 5 );
 
$content = curl_exec( $ch );
curl_close( $ch );
}
else
{
  die( 'No method to read from remote server was found.' );
}
return $content;
}
 
Then in the schedule_briefing and schedule_details, you can add the following:  $arrforecast=Schedulesdata::GetForecast($schedule->arricao);
 
To display it all nicely, add the folllowing code:
 
echo $depweather.$depsun."<br><br>".$arrweather.$arrsun;
echo "<br><br>TAF:".$arrforecast;
 
That's it! Your pilots are now informed about current and future weather conditions and sun times.