Author Topic: PHP scriptlet to add GMT for local time correlation  (Read 10024 times)

0 Members and 1 Guest are viewing this topic.

Offline Anole

  • Forecaster
  • *****
  • Posts: 585
    • http://pineislandweather.com
PHP scriptlet to add GMT for local time correlation
« on: March 02, 2007, 04:10:58 PM »
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: [Select]
Current GMT: <?= gmdate("Y/m/d - H:i:s") ?>

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: [Select]
<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>

Offline carterlake

  • Senior Contributor
  • ****
  • Posts: 243
    • CarterLake.org
Re: PHP scriptlet to add GMT for local time correlation
« Reply #1 on: March 02, 2007, 06:33:23 PM »
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)


Davis VP2 6153; Weather Display (LIVE w/ Ajax); Quickcam for Notebooks Pro; Boltek w/ Nexstorm; GRLevel3; live NOAA Radio

Offline anchorageweather

  • Forecaster
  • *****
  • Posts: 445
    • http://eetee.us/station/station.php
Re: PHP scriptlet to add GMT for local time correlation
« Reply #2 on: March 03, 2007, 08:38:17 AM »
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)



I can't get on the GRLevelX.com board. :(  Can someone post the php script that will convert GMT time to local time?!?
South of the Tracks, Anchorage, KY

Offline carterlake

  • Senior Contributor
  • ****
  • Posts: 243
    • CarterLake.org
Re: PHP scriptlet to add GMT for local time correlation
« Reply #3 on: March 03, 2007, 08:47:27 AM »
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?!?


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: [Select]

$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;


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: [Select]

<?
$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



?>

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: [Select]

<?
$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



?>


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: [Select]
$brown = imagecolorallocate($im, 138, 99, 68);
imagefilledrectangle($im, 466, 474, 506, 489, $brown);


And then added the converted time over the rectangle.

Code: [Select]
$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);


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: [Select]

<?php

$lastradar fetchthisUrlWithoutHanging&#40;'http&#58;//weather.noaa.gov/pub/SL.us008001/DF.of/DC.radar/DS.p19r0/SI.koax/sn.last'&#41;;
putenv&#40;'TZ=US/Central'&#41;;
$slice explode&#40;"KOAX ",$lastradar&#41;; 
$slice explode&#40;"N0ROAX",$slice[1&#93;&#41;; 
$lastradar=$slice[0&#93;; 

if &#40;strlen&#40;$lastradar&#41;>2&#41; &#123;

$vd=substr&#40;$lastradar, 0, 2&#41;; 
$vh=substr&#40;$lastradar, 2, 2&#41;; 
$vm=substr&#40;$lastradar, 4, 2&#41;; 

////////// GMT TO EPOCH CONVERSION 
$m gmdate&#40;'m'&#41;; 
$dgmdate&#40;'d'&#41;; 
$ygmdate&#40;'y'&#41;; 
if &#40;$vd < $d&#41; &#123; 
$m++; 
if &#40;$m > 12&#41; &#123; 
$m=1
$y++; 
&#125;
&#125;
$radarepoch gmmktime&#40;$vh,$vm,0,$m,$vd,$y&#41;;

$lastradartime date&#40;"g&#58;iA", $radarepoch&#41;;

&#125; else &#123;

$current_time time&#40;&#41;;
$lastradartime date&#40;"g&#58;iA",$current_time-120&#41;;

&#125;

echo $lastradartime;

function 
fetchthisUrlWithoutHanging&#40;$url&#41;
   
&#123;
   // Set maximum number of seconds &#40;can have floating-point&#41; 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&#40;0&#41;;

   // Extract resource path and domain from URL ready for fsockopen

   
$url str_replace&#40;"http&#58;//","",$url&#41;;
   
$urlComponents explode&#40;"/",$url&#41;;
   
$domain $urlComponents[0&#93;;
   
$resourcePath str_replace&#40;$domain,"",$url&#41;;

   // Establish a connection
   
$socketConnection fsockopen&#40;$domain, 80, $errno, $errstr, $numberOfSeconds&#41;;

   
if &#40;!$socketConnection&#41;
       
&#123;
       // You may wish to remove the following debugging line on a live Web site
       // print&#40;"<!-- Network error&#58; $errstr &#40;$errno&#41; -->"&#41;;
       
&#125;    // end if
   
else    &#123;
       
$xml '';
       
fputs&#40;$socketConnection, "GET $resourcePath HTTP/1.0\r\nHost&#58; $domain\r\n\r\n"&#41;;
   
       // Loop until end of file
       
while &#40;!feof&#40;$socketConnection&#41;&#41;
           
&#123;
           
$xml .= fgets&#40;$socketConnection, 128&#41;;
           
&#125;    // end while

       
fclose &#40;$socketConnection&#41;;

       
&#125;    // end else

   
return&#40;$xml&#41;;

   
&#125;    // end function

?>


My code -is- fault tolerant... it works even if NOAA is down.

Davis VP2 6153; Weather Display (LIVE w/ Ajax); Quickcam for Notebooks Pro; Boltek w/ Nexstorm; GRLevel3; live NOAA Radio

Offline carterlake

  • Senior Contributor
  • ****
  • Posts: 243
    • CarterLake.org
Re: PHP scriptlet to add GMT for local time correlation
« Reply #4 on: March 03, 2007, 09:37:26 AM »
So here's Anole's timestamp for KTBW..

Code: [Select]

<?php

$lastradar fetchthisUrlWithoutHanging&#40;'http&#58;//weather.noaa.gov/pub/SL.us008001/DF.of/DC.radar/DS.p19r0/SI.ktbw/sn.last'&#41;;
$slice explode&#40;"KTBW ",$lastradar&#41;; 
$slice explode&#40;"N0RTBW",$slice[1&#93;&#41;; 

putenv&#40;'TZ=US/Eastern'&#41;;

$lastradar=$slice[0&#93;; 

if &#40;strlen&#40;$lastradar&#41;>2&#41; &#123;

$vd=substr&#40;$lastradar, 0, 2&#41;; 
$vh=substr&#40;$lastradar, 2, 2&#41;; 
$vm=substr&#40;$lastradar, 4, 2&#41;; 

////////// GMT TO EPOCH CONVERSION 
$m gmdate&#40;'m'&#41;; 
$dgmdate&#40;'d'&#41;; 
$ygmdate&#40;'y'&#41;; 
if &#40;$vd < $d&#41; &#123; 
$m++; 
if &#40;$m > 12&#41; &#123; 
$m=1
$y++; 
&#125;
&#125;
$radarepoch gmmktime&#40;$vh,$vm,0,$m,$vd,$y&#41;;


$lastradartime date&#40;"g&#58;iA", $radarepoch&#41;;

&#125; else &#123;

$current_time time&#40;&#41;;
$lastradartime date&#40;"g&#58;iA",$current_time-120&#41;;

&#125;

echo $lastradartime;

function 
fetchthisUrlWithoutHanging&#40;$url&#41;
   
&#123;
   // Set maximum number of seconds &#40;can have floating-point&#41; 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&#40;0&#41;;

   // Extract resource path and domain from URL ready for fsockopen

   
$url str_replace&#40;"http&#58;//","",$url&#41;;
   
$urlComponents explode&#40;"/",$url&#41;;
   
$domain $urlComponents[0&#93;;
   
$resourcePath str_replace&#40;$domain,"",$url&#41;;

   // Establish a connection
   
$socketConnection fsockopen&#40;$domain, 80, $errno, $errstr, $numberOfSeconds&#41;;

   
if &#40;!$socketConnection&#41;
       
&#123;
       // You may wish to remove the following debugging line on a live Web site
       // print&#40;"<!-- Network error&#58; $errstr &#40;$errno&#41; -->"&#41;;
       
&#125;    // end if
   
else    &#123;
       
$xml '';
       
fputs&#40;$socketConnection, "GET $resourcePath HTTP/1.0\r\nHost&#58; $domain\r\n\r\n"&#41;;
   
       // Loop until end of file
       
while &#40;!feof&#40;$socketConnection&#41;&#41;
           
&#123;
           
$xml .= fgets&#40;$socketConnection, 128&#41;;
           
&#125;    // end while

       
fclose &#40;$socketConnection&#41;;

       
&#125;    // end else

   
return&#40;$xml&#41;;

   
&#125;    // end function

?>


http://www.carterlake.org/timetest.php

Davis VP2 6153; Weather Display (LIVE w/ Ajax); Quickcam for Notebooks Pro; Boltek w/ Nexstorm; GRLevel3; live NOAA Radio

Offline iam_clint

  • Member
  • *
  • Posts: 2
Re: PHP scriptlet to add GMT for local time correlation
« Reply #5 on: March 21, 2007, 01:02:22 PM »
So the only fix you guys have found is to just write php to show the time difference?

Offline Anole

  • Forecaster
  • *****
  • Posts: 585
    • http://pineislandweather.com
Re: PHP scriptlet to add GMT for local time correlation
« Reply #6 on: March 21, 2007, 01:11:52 PM »
Actually no, carterlake's solution writes the new time stamp onto the image itself. I chose not to do that because I use a total of 30 different images from GRlevel3 and didn't want to have to process all 30 images.

Offline iam_clint

  • Member
  • *
  • Posts: 2
Re: PHP scriptlet to add GMT for local time correlation
« Reply #7 on: March 21, 2007, 01:29:38 PM »
they don't offer the source code for grx level 3 do they.. (I don't own it, its for a local radio station)

Offline carterlake

  • Senior Contributor
  • ****
  • Posts: 243
    • CarterLake.org
Re: PHP scriptlet to add GMT for local time correlation
« Reply #8 on: March 21, 2007, 05:53:49 PM »
Quote from: "iam_clint"
So the only fix you guys have found is to just write php to show the time difference?


What sort of fix were you looking for? I actually output a new image with the time on that image... but it's quite complex.

Quote from: "iam_clint"
they don't offer the source code for grx level 3 do they.. (I don't own it, its for a local radio station)


Er... no.

Davis VP2 6153; Weather Display (LIVE w/ Ajax); Quickcam for Notebooks Pro; Boltek w/ Nexstorm; GRLevel3; live NOAA Radio

 

anything