Castle Rock Weather Staton - Castle Rock, Colorado
Fri 8/29/14 8:05:24pm MDT
Welcome Page » PHP Scripts

PHP Weather Scripts

This page shows some of the PHP scripts and code used to run our website. Some of this code was written by myself. Some of this code was adapted from other's code. I will do my best to show to give credit to those who originated the source of these PHP scripts.

Before we jump in I wanted to take a moment to explain PHP. PHP is a widely used general-purpose scripting language that is especially suited for web development and can be embedded into HTML. It generally runs on a web server, taking PHP code as its input and creating web pages as output. It can be deployed on most web servers and on almost every operating system and platform free of charge.

Most important thing to remember is PHP runs on the web server prior to the client's browser seeing the HTML code. So PHP does not show as part of the web page's source code. Almost all of the available weather site code is available in PHP. It makes sense to invest in learning more about this language. To learn more visit The PHP Group and simply search Google.


FTP Upload Status Scripts

I wrote these script a few months ago, to monitor the files I upload to my weather site on a regular basis. It works well but because it is PHP it only runs on page refresh. Make sure you set your html page to refresh every minute or so.

The first problem I had was my web server is in Central time zone, but my weather station is in Mountain Time zone. The code used to check file time stamps returns web server time not, Mountain Time. To resolve this you can use the "date_default_timezone_set" function.

This function is only available in PHP v5.0 or higher.

Insert the following at the top of our html page. I include in my header.php, which is the first included code when rendering any of my pages.Then I know all date functions will be in MST, instead of CST.

<?PHP date_default_timezone_set("America/Denver");?>

Replace "America/Denver" with your time zone identifier. The list of valid identifiers is available in the List of Supported Time Zones. Your date and time PHP code will now report your local time zone, if that was different then your web hosting company.

There are two more parts to my FTP Upload page code. I want to know first what the current date/time stamp of the file is. Second how old that file is from when the page was last loaded. These are 2 separate code snips, so you can use them in a HTML <table>.

The following snip of code us used to display the time stamp of the noaamo.txt file located in the same directory as the script. If your file is located in a another directory make sure you include the full path. For example "../test/file.txt" will go up a level and into the test directory, where file.txt resides. Another example would be "data/file.txt" would go down a level, where file.txt resides in the data directory.

<?PHP echo date("m/d/y H:i:s T", filectime("noaamo.txt"));?>

The previous script outputs the file time stamp as "m/d/y h:m:s" (i.e. 8/3/08 9:00:01 am). If you wish to change the way the date or time is displayed by altering the date() function. A list of valid parameters is available in the Date Function Manual.

The next snip of code is a function that calculates the number of seconds since the file changed, and converts that to min, hours, or days. In your webpage when you want to know the age of the file, simply include the following short snip of code

<?PHP echo timediff("data/noaamo.txt");?>

The timediff function, below, should be copied to the bottom of your webpage. This is the code that actually figures out the difference in time between the file time stamp, and current system time.

<?PHP

//==========================================================================

//  Function timediff - Shawn Curran - CastleRockWeather.org

//  version 1.0 - 08/11/08 - Initial Release

//  version 1.1 - 09/03/08 - Public Released version

//  Description: This function calculates the number of seconds, minutes,

//  hours, or days since the file's timestamp has been written. This works

//  well for files uploaded to your website via FTP, since the timestamp

//  reflects when the file was last written to the server with FTP client.

//==========================================================================

function timediff ($filepath) {

     //Get time stamp of file

     $t = date("mdyHis", filectime($filepath));

     //Split $t into month, day, and year values

     $m = substr($t, 0, 2);

     $d = substr($t, 2, 2);

     $y = substr($t, 4, 2);

     $h = substr($t, 6, 2);

     $min = substr($t, 8, 2);

     $s = substr($t, 10, 2);

     $timestamp = mktime($h, $min, $s, $m, $d, $y);

     $tdiff = mktime() - $timestamp; //Subtrack $timestamp from now, give us diff in seconds.

     //Determin how to display by size of timediff

     if ($tdiff < 60) { 

          //Timediff is less then 1 minute, so must be seconds

          $tdiff=number_format($tdiff);

          return $tdiff." sec";

     } elseif ($tdiff < 3600) { 

          //timediff is less then 1 hour, so must be minutes

          $tdiff=$tdiff/60;

          $tdiff=number_format($tdiff, 1, '.', '');

          return $tdiff." min";

     } elseif ($tdiff < 216000) { 

          //timediff is less then 1 day, so must be hours.

          $tdiff=($tdiff/60)/60;

          $tdiff=number_format($tdiff, 1, '.', '');

          return $tdiff." hr";

     } else { 

          //timediff is greater then 1 day, so must be days.

          $tdiff=(($tdiff/60)/60)/24;

          $tdiff=number_format($tdiff, 1, '.', '');

          return $tdiff." days";

     }

}

?>

To see examples using this code, download lastuploaded.zip.


Hamvid Presentation Script

I was inspired by the layout of hamvid on Cape Coral Online Weather's website. I love his layout, but didn't want to manage 3 completely different webpages to show the different types of videos. To solve this problem I created a basic script to accomplish the same thing. My first version of this script was the simple IF statement part of the code. It helped format the link, and some of the video URL. The version I am releasing to our community is more complex and allows you to easily configure the code to match your account number, video size, audio pref, color scheme, zip code, and url. This page was tested on PHP v5.0, but should work on v4.x also. I don't believe I'm using 5.x code.

To use this script download the hamvid.zip file, by clicking on the "Download" link below. Save to your website. Include the php page using the following code:

<?PHP include("hamvid.php");?>

Download | Demo