Reputation: 979
I am trying to run a certain batch of code only during certain hours of the day. I want to run this code only after 8:00am and before 10:00 pm.
This code is included in the footer of my site, but it doesn't seem to be working, it is still running the code through these hours. (my server is in the same time zone as I am):
if(date("Hi") < 2200 && date("Hi") > 0800){
// Run Code
}
How can I run the code only between the specific times?
Upvotes: 5
Views: 3202
Reputation: 12719
Try with strtotime function and relative formats:
if ( time() > strtotime( '08:00AM' ) && time() < strtotime( '10:00PM' ) ) {
// run code
}
Upvotes: 3
Reputation: 10939
Like Ryan said up in the comments - You need to set this up as a cron job.
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/
to see your crons:
crontab -l
edit the crontab
crontab -e
an example cron that runs at 8am daily
00 08 * * * php path/to/script.php
The problem with setting it up the way you are attempting to is that every single request to your site is going to set off the event again (a possibility of mucking up the transactions)
Upvotes: 1
Reputation: 1488
just use "G" to get hours without leading 0
$time = date("Gi");
if(800 <= $time && $time <= 2200){
// Run Code
}
Upvotes: 0