meinrobert Posted August 27, 2011 Share Posted August 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245815-please-help-3-hour-refresh-problem/ Share on other sites More sharing options...
DavidAM Posted August 27, 2011 Share Posted August 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245815-please-help-3-hour-refresh-problem/#findComment-1262596 Share on other sites More sharing options...
meinrobert Posted August 27, 2011 Author Share Posted August 27, 2011 Thank you very much. I may later try to develop this more by having both the past image, the future image, and possibly a third selection rotate rapidly through javascript. This works perfectly now though, and was a big help. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/245815-please-help-3-hour-refresh-problem/#findComment-1262700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.