Search code examples
phpxmlif-statementtimelimit

PHP if, just once every 30 min?


I have a script with a IF statement, which checks a XML for specific values. This document is refreshed every 30 seconds, and if the value is found in the XML - then execute the command. Is there any way to make the script just execute once every 15 minutes, and not EVERY time the XML is refreshed every 30 seconds?

$xml = 'http://www.example.com/xmlfile.xml';

if ($xml->value == '1')
{
// Exectute something if value from the XML is 1.

}
else
{
// Value not 1, do nothing.

}

Solution

  • $fileName = "lastrun.txt";
    $time = file_get_contents($filename);
    $timeBetweenRuns = 15;
    if(!$time || time() > $time+60*$timeBetweenRuns){//if no file or time elapsed more then timeBetweenRuns 
       //Your code read file
       if($xml->1){
           //Your code .. Only thing if it may take more then next run, you need to add lock to not allow two process do same thing
           file_put_contents($filename, time())
       }
       else{//Your code
       }
    }
    

    Something like that.