We have a plugin which consists of two simple files, the plugin loader php file which take cares of plugin name, and version etc. and a functions.php which is called from the plugin loader file.
The functions.php is where the actual code is that does the work. The plugin is responsible for updating WooCommerce tables with stock from a CSV file every hour.
I want to change this so it only operates between the hours of 9am and 5pm, Monday to Friday.
I found this: PHP script to execute at certain times
and modified my script by enclosing the main execution block in an If conditional like so:
$file_url = plugin_dir_path( __FILE__ );
$hour = date('G'); // 0 .. 23
if ($hour >= 9 && $hour <= 17 && date('w') != 0 && date('w') !=6 ) {
if(file_exists($file_url.'/csv_cron.txt')){
$get_time = fopen($file_url.'/csv_cron.txt', "r") or die("Unable to open file!");
$timer = fgets($get_time);
$last_run = intval((time()-$timer)/60);
fclose($get_time);
if($last_run > 60){
w3ip_updater();
$myfile = fopen($file_url."/csv_cron.txt", "w") or die("Unable to open file!");
$txt = time();
fwrite($myfile, $txt);
fclose($myfile);
}
}else{
$myfile = fopen($file_url."/csv_cron.txt", "w") or die("Unable to open file!");
$txt = time();
fwrite($myfile, $txt);
fclose($myfile);
}
}
The function w3ip_updater() is also in this file below the above code block but I omitted it as it didn't seem relevant to my question.
So I have added the if conditional as I have demonstrated above, yet the function is still operating even after 5pm. It's now 7PM and the file already updated at 6:34pm, why is the above code still executing, past 5pm, given I have wrapped it in the if conditional which says only execute if the time is between 9am and 5pm?