3 Hour Refresh Code Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
meinrobert
Forum Newbie
Posts: 1
Joined: Sat Aug 27, 2011 7:35 am

3 Hour Refresh Code Problem

Post 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.
greip
Forum Commoner
Posts: 39
Joined: Tue Aug 23, 2011 8:23 am
Location: Oslo, Norway

Re: 3 Hour Refresh Code Problem

Post 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)) );
Post Reply