rickphp Posted February 29, 2012 Share Posted February 29, 2012 Hi, I need a basic if function to run based on time. How it will work is, the script is called. If the script was ran in the last 30 seconds then nothing happens, else it carries out the function. The last access time would have to be stored so the script could see if the current time is more than 30 seconds past the time in the file, a simple text file would be sufficient for this. Can anyone advise how I'd achieve this? Thanks for any help! Quote Link to comment Share on other sites More sharing options...
sunfighter Posted February 29, 2012 Share Posted February 29, 2012 Used a file like you said: <?php $handle = fopen('time.txt', 'r'); $old_time = fread($handle, 10); fclose($handle); $now_time = time(); $final_time = ($now_time - $old_time); if($final_time > 30) // The program you want to run goes here { for($i = 0; $i < 50000; $i++) { $bug = 'lets put some things in it'; $bug1 = 'lets put two things in it'; $bug2 = 'lets put three things in it'; $bug3 = 'lets put more things in it'; } echo $bug.'<br />'; echo $bug1.'<br />'; echo $bug2.'<br />'; echo $bug3.'<br />'; }else{ echo 'You must wait';die; } $time_end = time(); $handle = fopen('time.txt', 'w'); fwrite($handle, $time_end, 10); fclose($handle); ?> Quote Link to comment Share on other sites More sharing options...
rickphp Posted February 29, 2012 Author Share Posted February 29, 2012 Thank you very much for that. One question though, what is this code for? for($i = 0; $i < 50000; $i++) { Its confuzzled me a bit.. Would it work like this? <?php $handle = fopen('time.txt', 'r'); $old_time = fread($handle, 10); fclose($handle); $now_time = time(); $final_time = ($now_time - $old_time); if($final_time > 30) { function would go here.. }else{ echo 'You must wait';die; } $time_end = time(); $handle = fopen('time.txt', 'w'); fwrite($handle, $time_end, 10); fclose($handle); ?> Quote Link to comment Share on other sites More sharing options...
sunfighter Posted March 1, 2012 Share Posted March 1, 2012 The for loop was just something to do. Replace it with you program Quote Link to comment Share on other sites More sharing options...
DavidAM Posted March 1, 2012 Share Posted March 1, 2012 You can avoid the overhead of reading and writing the file by using the touch function. This function just changes the access and modification times on a file. If the file does not exist, it will be created. It is not uncommon on *nix systems to have an empty file around just to have a place to store a date-time stamp. if (file_exists($checkFile)) $lastTime = filemtime($checkfile); else $lastTime = 0; // Never been run before if ((time() - $lastTime) > 30) { // More than 30 seconds ago // Run your process here // The last step of the process should be ... touch($checkFile); } I would put some text inside the file explaining why it is there; you know, like the "This page intentionally left blank" pages in a technical document. Quote Link to comment Share on other sites More sharing options...
rickphp Posted March 1, 2012 Author Share Posted March 1, 2012 Thanks for your help guys. Quote Link to comment Share on other sites More sharing options...
sunfighter Posted March 2, 2012 Share Posted March 2, 2012 Nice one DavidAM. 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.