0

I am trying to allow my simple website to check the time, so that if the set alarm time is equal to the real time the alarm goes off.

I'm using PHP (and I am pretty sure it must be in PHP due to using LEDs and python). I know this is relatively easy thing to do in js for example.

I have the variables:

$setTime = "$hour$hour2:$minutes$minutes2";

$realTime = date("H:i");

and a if statement:

if ($realTime == $setTime) { exec("sudo python /home/pi/lighton_1.py"); }

else{ exec("sudo python /home/pi/lightoff_1.py"); }

This all works if when I load my website the real time = the set time however if not it won't. I somehow really want to check the if statement so often somehow. I have tried loops, functions etc and haven't had much success however my coding is a bit basic at the moment. Wondering if anyone knows this solution, (could be very simple?) Need help fast please. Thank you!

Rory
  • 1
  • 1

1 Answers1

0

You need to understand how your PHP code is triggered. When used to host a website, it is triggered, when someone requests a page. Now, what are the chances than someone will request this page when "$realTime == $setTime". Unless you host a very busy site, the chances are very small. You could have a web page run a JavaScript to continuously refresh the page. Even in this case you may want to say "$realTime > $setTime".

You could alternatively run your PHP from a scheduler like a Unix Cron job, or some kind of PHP scheduler, but then you have to say "$realTime > $setTime", because scheduler may also not run this statement at the exact moment.

Ari Singh
  • 1,228
  • 7
  • 12
  • Would this be the write theory to it? $realTime > $setTime || $realtime < $setTime ? Refreshing the page works however deletes the users input, therefore not working as a result. I will try looking for a scheduler as you say – Rory Mar 06 '18 at 16:16
  • If anyone knows how to use a Cron Job in this scenario this would be a massive help – Rory Mar 06 '18 at 17:11
  • This should help: https://stackoverflow.com/questions/22358382/execute-php-script-in-cron-job – Ari Singh Mar 06 '18 at 17:52
  • After researching into this I have had no luck with schedules as the set alarm has to be able to be changed by the user, not at set every 24 hours for example. Any ideas of how to possibly get around this or any other ways would really help. Thank you – Rory Mar 06 '18 at 22:12
  • If you want a user interface then you should go with a java script solution, that will call the PHP, when the time is right. Cron does not allow user interface. Then the check "$realTime > $setTime" can also be done in JS. Or, you can get the $setTime for env. variable and have the cron run the job every 5 mins, but only do things if $realTime - $setTime > 5 minutes - so that it runs every 5 mins, but will do something only once - rest of the time when it runs - it will do nothing. – Ari Singh Mar 06 '18 at 22:19