Jump to content

Filemtime adjustment


Texan78

Recommended Posts

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>";
}

?>
Link to comment
Share on other sites

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
}
Link to comment
Share on other sites

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

  • Like 1
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.