Weather Software > GRLevel X Software
PHP scriptlet to add GMT for local time correlation
Anole:
This may be moot but since I was unable to find a way to get the timestamp on the radar image to be anything but GMT, I decided to add a bit of PHP code to output the current GMT to make it easier for visitors to correlate the displayed image to the current time. Moot because there may be a way in GRlevel3 to change that timestamp and I just couldn't find it. Regardless here's a one liner to display the current GMT:
--- Code: ---Current GMT: <?= gmdate("Y/m/d - H:i:s") ?>
--- End code ---
This assumes proper time setup on the server itself. If the time has not been setup properly you may need to add an offset to the above.
If you define your styles for the page correctly you can make it line up right under the timestamp in the image to make it pretty easy for the visitor. You can see this here:
http://weather.anolecomputer.com/radar/radar_base.php
Here's the markup I'm using to get things lined up. It takes into acount some other things I'm doing on the page (like the margins of the containing div) so you'd probably need to make some modifications, but you'll get the idea:
--- Code: ---<div style="text-align:right;font-weight:bold;font-size:10pt;margin-right:5px">Current GMT: <?= gmdate("Y/m/d - H:i:s") ?>Z </div>
--- End code ---
carterlake:
You gotta keep up...
http://www.grlevelx.com/owners/viewtopic.php?t=2890
(By the way, I started using Jeff's preg_match code but went back to my own as I found it more reliable in the long run. I also added a bit of code so that if the fetch takes a crap (NOAA down again) then it adds current time minus three minutes to the image)
anchorageweather:
--- Quote from: "carterlake" ---You gotta keep up...
http://www.grlevelx.com/owners/viewtopic.php?t=2890
(By the way, I started using Jeff's preg_match code but went back to my own as I found it more reliable in the long run. I also added a bit of code so that if the fetch takes a crap (NOAA down again) then it adds current time minus three minutes to the image)
--- End quote ---
I can't get on the GRLevelX.com board. :( Can someone post the php script that will convert GMT time to local time?!?
carterlake:
--- Quote from: "anchorageweather" ---I can't get on the GRLevelX.com board. :( Can someone post the php script that will convert GMT time to local time?!?
--- End quote ---
Mike mentioned something the other day when we were going through the crisis with the NOAA servers....
He mentioned where the files were located.
http://weather.noaa.gov/pub/SL.us008001/DF.of/DC.radar/DS.p19r0/
In those files is a TIMESTAMP! in text.
http://weather.noaa.gov/pub/SL.us008001/DF.of/DC.radar/DS.p19r0/SI.koax/sn.last
Hello PHP.
Pull timestamp... reformat to my time... and VOILA! timestamp in local time.
--- Code: ---
$lastradar = implode('', 'http://weather.noaa.gov/pub/SL.us008001/DF.of/DC.radar/DS.p19r0/SI.koax/sn.last');
$slice = explode("KOAX ",$lastradar);
$slice = explode("N0ROAX",$slice[1]);
$lastradar=$slice[0];
$days=substr($lastradar, 0, 2);
$hours=substr($lastradar, 2, 2);
$minutes=substr($lastradar, 4, 2);
$lastradarUTC = mktime($hours, $minutes, 0, $days);
putenv('TZ=US/Central');
$lastradartime = date("g:i A", $lastradarUTC);
echo $lastradartime;
--- End code ---
I have it functioning in test on my current radar image.
Then Jeff an MichiganWxSystem.com posted...
tested on php 5.2 .. and according to the php website will work on
all versions of php 5
--- Code: ---
<?
$radar = "kapx";
$tz = "-5";
$fp = fopen("http://weather.noaa.gov/pub/SL.us008001/DF.of/DC.radar/DS.p19r0/SI.".$radar."/sn.last", "r");
$data =stream_get_contents($fp);
if (preg_match('/(\d\d)(\d\d)(\d\d)/' , $data , $mm)){
$vd = $mm[1]; $vh = $mm[2]; $vm = $mm[3];}
////////// GMT TO EPOCH CONVERSION
$m = gmdate('m');
$d= gmdate('d');
$y= gmdate('y');
if ($vd < $d) {
$m++;
if ($m > 12) {
$m=1;
$y++;
}
}
$radarepoch = gmmktime($vh,$vm,0,$m,$vd,$y);
$timestamp = gmdate('m/d/Y g:i A' , $radarepoch+$tz*3600);
echo $timestamp
?>
--- End code ---
results http://www.michiganwxsystem.com/test/time.php
And he posted...
php 4 version ....
(actually this will work on php 4.4.4 and above to the current of php 5.2.0)
--- Code: ---
<?
$radar = "kapx";
$tz = "-5";
$vd='';$vh='';$vm='';$data='';
$url = "http://weather.noaa.gov/pub/SL.us008001/DF.of/DC.radar/DS.p19r0/SI.".$radar."/sn.last";
$fp = fopen($url, "r");
while (!feof($fp)){
$data .= fread($fp, 8192);}
if (preg_match('/(\d\d)(\d\d)(\d\d)/' , $data , $mm)){
$vd = $mm[1]; $vh = $mm[2]; $vm = $mm[3];}
////////// GMT TO EPOCH CONVERSION
$m = gmdate('m');
$d= gmdate('d');
$y= gmdate('y');
if ($vd < $d) {
$m++;
if ($m > 12) {
$m=1;
$y++;
}
}
$radarepoch = gmmktime($vh,$vm,0,$m,$vd,$y);
$timestamp = gmdate('m/d/Y g:i A' , $radarepoch+$tz*3600);
echo $timestamp
?>
--- End code ---
if you wanted to just return time, remove the m/d/y in the $timestamp variable set
And then I posted...
I went ahead and masked the tilt with a rectangle...
--- Code: ---$brown = imagecolorallocate($im, 138, 99, 68);
imagefilledrectangle($im, 466, 474, 506, 489, $brown);
--- End code ---
And then added the converted time over the rectangle.
--- Code: ---$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagestring($im,$font,468,475,$text,$black);
imagestring($im,$font,467,474,$text,$white);
--- End code ---
I did my darnest to match the bitmap font on the radar... it's acceptable.
And finally... here's my current code (which is specific to KOAX... you'd have to change the explodes to be your particular radar site.
--- Code: ---
<?php
$lastradar = fetchthisUrlWithoutHanging('http://weather.noaa.gov/pub/SL.us008001/DF.of/DC.radar/DS.p19r0/SI.koax/sn.last');
putenv('TZ=US/Central');
$slice = explode("KOAX ",$lastradar);
$slice = explode("N0ROAX",$slice[1]);
$lastradar=$slice[0];
if (strlen($lastradar)>2) {
$vd=substr($lastradar, 0, 2);
$vh=substr($lastradar, 2, 2);
$vm=substr($lastradar, 4, 2);
////////// GMT TO EPOCH CONVERSION
$m = gmdate('m');
$d= gmdate('d');
$y= gmdate('y');
if ($vd < $d) {
$m++;
if ($m > 12) {
$m=1;
$y++;
}
}
$radarepoch = gmmktime($vh,$vm,0,$m,$vd,$y);
$lastradartime = date("g:iA", $radarepoch);
} else {
$current_time = time();
$lastradartime = date("g:iA",$current_time-120);
}
echo $lastradartime;
function fetchthisUrlWithoutHanging($url)
{
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
$numberOfSeconds=4;
// Suppress error reporting so Web site visitors are unaware if the feed fails
error_reporting(0);
// Extract resource path and domain from URL ready for fsockopen
$url = str_replace("http://","",$url);
$urlComponents = explode("/",$url);
$domain = $urlComponents[0];
$resourcePath = str_replace($domain,"",$url);
// Establish a connection
$socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds);
if (!$socketConnection)
{
// You may wish to remove the following debugging line on a live Web site
// print("<!-- Network error: $errstr ($errno) -->");
} // end if
else {
$xml = '';
fputs($socketConnection, "GET $resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n");
// Loop until end of file
while (!feof($socketConnection))
{
$xml .= fgets($socketConnection, 128);
} // end while
fclose ($socketConnection);
} // end else
return($xml);
} // end function
?>
--- End code ---
My code -is- fault tolerant... it works even if NOAA is down.
carterlake:
So here's Anole's timestamp for KTBW..
--- Code: ---
<?php
$lastradar = fetchthisUrlWithoutHanging('http://weather.noaa.gov/pub/SL.us008001/DF.of/DC.radar/DS.p19r0/SI.ktbw/sn.last');
$slice = explode("KTBW ",$lastradar);
$slice = explode("N0RTBW",$slice[1]);
putenv('TZ=US/Eastern');
$lastradar=$slice[0];
if (strlen($lastradar)>2) {
$vd=substr($lastradar, 0, 2);
$vh=substr($lastradar, 2, 2);
$vm=substr($lastradar, 4, 2);
////////// GMT TO EPOCH CONVERSION
$m = gmdate('m');
$d= gmdate('d');
$y= gmdate('y');
if ($vd < $d) {
$m++;
if ($m > 12) {
$m=1;
$y++;
}
}
$radarepoch = gmmktime($vh,$vm,0,$m,$vd,$y);
$lastradartime = date("g:iA", $radarepoch);
} else {
$current_time = time();
$lastradartime = date("g:iA",$current_time-120);
}
echo $lastradartime;
function fetchthisUrlWithoutHanging($url)
{
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
$numberOfSeconds=4;
// Suppress error reporting so Web site visitors are unaware if the feed fails
error_reporting(0);
// Extract resource path and domain from URL ready for fsockopen
$url = str_replace("http://","",$url);
$urlComponents = explode("/",$url);
$domain = $urlComponents[0];
$resourcePath = str_replace($domain,"",$url);
// Establish a connection
$socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds);
if (!$socketConnection)
{
// You may wish to remove the following debugging line on a live Web site
// print("<!-- Network error: $errstr ($errno) -->");
} // end if
else {
$xml = '';
fputs($socketConnection, "GET $resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n");
// Loop until end of file
while (!feof($socketConnection))
{
$xml .= fgets($socketConnection, 128);
} // end while
fclose ($socketConnection);
} // end else
return($xml);
} // end function
?>
--- End code ---
http://www.carterlake.org/timetest.php
Navigation
[0] Message Index
[#] Next page
Go to full version