Texan78 Posted November 13, 2014 Share Posted November 13, 2014 Hello, I have a small script that uses filemtime. It checks the last updated time and if it within X amount of time it will show ONLINE otherwise it will show OFFLINE. Currently it is set for two hours since I built this off the php examples which used two hours. How can I adjust this so that if it is 15 mins old it shows OFFLINE instead of 2 hours? Here is the code I am using for example. -Thanks <?php //Styling of text $offline = "color:#FF0000; font-weight: bold; font-size:14px;"; $online = "color:#228B22; font-weight: bold; font-size:14px;"; //Check age of image and if older then 15 mins display offline otherwise online $filename = './gr3/animatedcr.gif'; if (time()-filemtime($filename) > 2 * 3600) { echo "<div style='$offline'> OFFLINE</div>"; } else { echo "<div style='$online'> ONLINE</div>"; } ?> Quote Link to comment Share on other sites More sharing options...
Texan78 Posted November 13, 2014 Author Share Posted November 13, 2014 I believe i have found my answer. Would this be correct? 15 * 60 Quote Link to comment Share on other sites More sharing options...
gizmola Posted November 13, 2014 Share Posted November 13, 2014 You really can't pick out the part that compares the time? 2 * 3600 look familiar to you? 3600 = seconds in an hour. You should be able to figure out how to come up with the number of seconds in 15 minutes right? Quote Link to comment Share on other sites More sharing options...
gizmola Posted November 13, 2014 Share Posted November 13, 2014 Correct. I personally see no reason to force a computation there, when it's a constant, but what you have will work. 900 would be better (with a comment) above to explain the constant. You can also actually use constants for this, and eliminate magic numbers. Even better. define('OFFLINE_SECONDS', 900); // 15 Minutes // comparison if (time() - filemtime($filename) > OFFLINE_SECONDS) { // Show offline } else { // Show online } Quote Link to comment Share on other sites More sharing options...
Texan78 Posted November 13, 2014 Author Share Posted November 13, 2014 You really can't pick out the part that compares the time? 2 * 3600 look familiar to you? 3600 = seconds in an hour. You should be able to figure out how to come up with the number of seconds in 15 minutes right? Thanks, I knew where I need to make the change I just wasn't aware of I need to format the change since I needed it in mins instead of hour so 15 * 3600 wouldn't work because it would be 15 hrs. I am not real familiar with using magic numbers so wasn't sure. I did apply your suggestion as I do like that method better. -Thanks 1 Quote Link to comment 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.