0

I wanted to run a php script every hour and needs to stop the script from night 8PM to morning 8AM.

Part of the code which I have created

$hour = date('G');
print $hour;
if ($hour >= 21 && $hour <=8)
{
die("Script is halted.\n");
}

cron job is set to run evey hour. But for some reason, it is running every hour not stopping from 8PM to 8AM.

0

3 Answers 3

4

Seems silly to add conditional statements in your code, when you can simply just setup your cron to only run between 9AM to 7PM:

0 9-19 * * * php /var/www/vhosts/domain.com/httpdocs/scripts/example.php

See crontab.guru for explanation of this. On windows, you can simply set the hours to run in the scheduled task.

Sign up to request clarification or add additional context in comments.

4 Comments

I agree, it seems better to let the scheduler handle the scheduling.
However, I suppose they could be running only part of their script conditionally (although it doesn't sound like that's the case).
IMO: Create 2 scripts then. Also, from the way the OP describes this, it appears like he's attempting to stop the entire script. This is a solid alternative though, and removes unnecessary conditionals.
Well, yes. I still stand by my original comment, regardless of any other speculation. I think this is the best way to go.
2

Change:

if ($hour >= 21 && $hour <=8)

to:

if (($hour >= 21) or ($hour <=8))

or

if (($hour >= 21) || ($hour <=8))

&& == and

Comments

0
date_default_timezone_set('UTC');
$New_Time = time() + (6 * 60 * 60);
$Hour = date("G",$New_Time);
if((8>$Hour ||  9<$Hour) && (14>$Hour || 15<$Hour) && (20>$Hour || 21<$Hour)){
    echo "Your Codes Here
    Works nicely On PHP
    But change my timestamps.!";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.