-1

I have an IF statement that'll only run when I reload the page. Here's the code:

if ($date == date('00:00:00') && (isset($postchecktoday))){
// Code goes here
}

The if statement is within a function that runs constantly. When it hits 00:00:00, I have to reload the page dead-on that time so that the if statement actually runs. I basically want the if statement to run by itself without having to reload the page at 00:00:00.

The whole point of it running precisely at 00:00:00 is so that it only runs the IF statement once every 24 hours.

Is there an alternative way of doing this using strictly PHP? I would use a CRON but wouldn't that execute the whole PHP file once a day? At the moment I have a function that's constantly running with code, and an if statement within that function which only runs at 00:00:00. Unless the CRON is able to directly call that IF statement from the PHP file, I'm unsure if that is any use to me.

  • 2
    this is a terrible idea... are you running this on a Linux server? Either way, have a CRON task run at midnight and run the script. – Ice76 Sep 13 '17 at 21:43
  • I'm unfamiliar with AJAX, I haven't tried it. And yes, this is on a Linux server. – user8426498 Sep 13 '17 at 21:44
  • 1
    CRON would be the best solution. – Mohammed Akhtar Zuberi Sep 13 '17 at 21:46
  • I've updated my original question regarding CRONs. – user8426498 Sep 13 '17 at 22:22
  • How are you using a PHP script that runs constantly from a web browser? Doesn't the browser time out waiting for the output? – Barmar Sep 13 '17 at 23:06
  • Scripts that need to run constantly shouldn't be run from a browser. You can run it from the server using `/etc/inittab` or `systemd`. – Barmar Sep 13 '17 at 23:06
  • It doesn't constantly run from a web browser. It's a plugin to a forum on a server. Just before the if statement I have a clock. The condition on the if statement is that at 00:00:00 the code within it executes. The if statement is constantly checking the time, but when it hits 00:00:00 the code doesn't execute without me or someone else reloading the forum page at precisely 00:00:00 for some reason. – user8426498 Sep 13 '17 at 23:17
  • Is there a better way of executing an if statement at 00:00:00? – user8426498 Sep 13 '17 at 23:18
  • @user8426498 That is how PHP runs. The script only runs when there is an action to trigger it. With web applications, a user clicking on something is that action. Then PHP responds. In your case, you click at 00:00:00. If you or someone else doesnt, it wont happen. – Ice76 Sep 13 '17 at 23:41

1 Answers1

3

If you need to keep it seperate, create the PHP script and use CRON to execute it once a day, at hour 0, minute 0, second 0.

Reference this

Ice76
  • 1,143
  • 8
  • 16
  • Is there an alternative way of doing this using strictly PHP? I assume that a CRON would execute the whole PHP file once a day? At the moment I have a function that's constantly running with code, and an if statement within that function which only runs at 00:00:00. Unless the CRON is able to directly call that IF statement from the PHP file, I'm unsure if this is any use to me. Forgive me if I'm wrong. – user8426498 Sep 13 '17 at 22:13
  • @user8426498 sorry for late reply. You can easily put the block of code into its own php file and run that file specifically. – Ice76 Sep 13 '17 at 22:37
  • @user8426498 Using strictly PHP, you would need a persistent data recording the date of the last time it ran. The thing is, equating 00:00:00 to the current time will not work 100% of the time. You computer may lag for a second, even if you load that page. You can use a database field and see if that than day, then, if not, run it and change that database entry. – Ice76 Sep 13 '17 at 22:38