Run code only between specific hours

I try to run a specific batch of code only at certain hours of the day. I want to run this code only after 8:00 and until 22:00.

This code is included in the footer of my site, but it does not seem to work, it still runs the code after this hours. (my server is in the same time zone as me):

if(date("Hi") < 2200 && date("Hi") > 0800){ // Run Code } 

How can I run code only between specific times?

+4
source share
4 answers
 if(date("Hi") < 2200 && date("Hi") > 800){ // Run Code } 
+6
source

As Ryan said in the comments - you need to set this as a cron job.

http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

To see your crowns:

 crontab -l 

edit crontab

 crontab -e 

cron example that runs at 8am daily

 00 08 * * * php path/to/script.php 

The problem with setting up this way that you are trying is that every single request to your site is going to send an event again (the ability to drop transactions)

+1
source

Try using the strtotime function and relative formats:

 if ( time() > strtotime( '08:00AM' ) && time() < strtotime( '10:00PM' ) ) { // run code } 
+1
source

just use "G" to get the watch without specifying 0

 $time = date("Gi"); if(800 <= $time && $time <= 2200){ // Run Code } 
0
source

Source: https://habr.com/ru/post/1483607/


All Articles