Jump to content

Please Help, 3 Hour Refresh Problem


meinrobert

Recommended Posts

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:

 

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

Link to comment
Share on other sites

Rather than use a couple of IF statements listing out 16 or's, you can use the modulus operator with a little math.

date_default_timezone_set('Etc/GMT-1');

$hour = date("H");	// Current Hour

// If the hour is NOT a "3" hour, increase it to the next "3" hour
if ($hour % 3) $hour += 3 - ($hour % 3);

// The math turns it into an integer, so make sure it is 2-digits 
if ($hour < 10) $hour = '0' . $hour;

// Get the date and tack the hour on the end
$today = date("Ymd") . $hour;

$pattern = '/WaveHeight_'.$today.'_mic.png';

 

The % is the modulus operator. It returns the remainder when (in this case) $hour is divided by 3. The if condition will always be zero -- false -- at 0, 3, 6, 9, 12, 15, 18, 21.  At the other hours, we add 3 to move forward then subtract the modulus value, which leaves us with a "3" hour.

 

Since we have done integer math on the $hour we have to turn it back into a 2-digit string if the value is less than ten.

 

Originally, I thought you were looking for the most recently PAST "3" hour, but then I checked the site and they do have FUTURE maps. If  you wanted the most recently PAST "3" hour we change one line of code (well, two if you count the comment)

 


#// If the hour is NOT a "3" hour, increase it to the next "3" hour
#if ($hour % 3) $hour += 3 - ($hour % 3);

// If the hour is NOT a "3" hour, subtract the modulus to get the last "3" hour
$hour -= ($hour % 3);	// Use to get the hour in the PAST 

 

we don't need an IF here, since any 3 hour modulus 3 returns zero and we are just subtracting zero from the hour.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.