Page 1 of 1

3 Hour Refresh Code Problem

Posted: Sat Aug 27, 2011 7:38 am
by meinrobert
Hello,

I am trying to load an external image from a NOAA site directory. This image refreshes every three hours, starting with 0 and then 3,6,9,12.....21 in a 24 hour calendar, and are stored for days in advance. The date/time is in the file name. Originally I had written the script to refresh on the hour, and then realized the tri-hourly spacing. This is what I have:

Code: Select all

<?php
date_default_timezone_set('Etc/GMT-1');
$today = date("YmdH");
$pattern = '/WaveHeight_'.$today.'_mic.png';
$base_url = 'http://www.crh.noaa.gov/images/greatlakes/ndfd/MIC/dynamic2';
print '<a href="http://www.crh.noaa.gov/greatlakes/?c=map&l=lm&p=a"><img src="'.$base_url.$pattern.'" width= "237" hieght= "314" " ></a>';
?> 
I am not fluent in php, but I assume I need a "If H=1,4,7,10,13,16,19,22; Then H+2 ; along with If H=2,5,8,11,14,17,20,23; Then H+1" to keep the images relatively current. I just can't quite wrap my head around how to express this.

Any help is appreciated.

Thank you.

Re: 3 Hour Refresh Code Problem

Posted: Sat Aug 27, 2011 11:17 am
by greip
The way you present your code it seems to be for a web page which should show the latest available NOAA image of the type you are interested in. The filename include year, month, day and hour when the image was produced. You know that a new file is produced approximately every third hour.

The problem you want to solve is how to generate the filename matching the latest available image.

What you are looking for is something like an analogue clock where the hands move only every third hour. This is easy to achieve by representing the current time in seconds and then using the modulus operator (%) to compute how many seconds into the current three hour period we are. Then subtract that number of seconds from the current time before formatting it to text.

Code: Select all

$now = time ();
$today = date ( "YmdH", $now - ($now % (3 * 60 * 60)) );