-2

Hello Everyone,

i want to make something like time scheduler with php and insert if statement in it

example:

<?php
$hours = "Time Code here";
if($hours = 24H ) {
//my code
} else {
//code
}
?>

what i want make this code happen in every 24H

how i can make this code work ?

Lil Zyz
  • 25
  • 9
  • 1
    Why not use a cron job? – rickdenhaan Sep 10 '17 at 01:54
  • @rickdenhaan what does that mean – Lil Zyz Sep 10 '17 at 01:55
  • 1
    With cron jobs (Linux/Unix) or scheduled tasks (Windows) you ask the operating system to call your PHP script automatically every X minutes. You can easily set that to happen once every 24 hours. – rickdenhaan Sep 10 '17 at 01:57
  • @LilZyz read the answer thread:-https://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php – Alive to die - Anant Sep 10 '17 at 01:57
  • i got it sorry, but idon't have sql in this site + i want do something in my head with this code – Lil Zyz Sep 10 '17 at 01:57
  • 1
    @LilZyz, running a job every 24 hours is not possible with a simple php script unless something triggers the run of this script, either you or a cron job. but if you put it on a very high traffic site for instance, then you can be sure a visitor will trigger your script very shortly after 24 hours. So i mean you may not need a cron job if your task to perform is light and if the user triggering it (like the first user after midnight) is not noticing a slow down. – antoni Sep 10 '17 at 02:00
  • @antoni i don't want it for visitors i want every 24 the file doing a job – Lil Zyz Sep 10 '17 at 02:03
  • @antoni u can make it easily with file_put & get but i have brainlag right now – Lil Zyz Sep 10 '17 at 02:04
  • 2
    file_put does not exist, your talking about file_put_contents and it still wont trigger every 24h by itself, even if your brain is on. – antoni Sep 10 '17 at 02:06
  • i tried to use cron job but it was too difficult even with stackoverflow question i didn't can do it – Lil Zyz Sep 10 '17 at 02:19
  • Possible duplicate of [How to create cron job using PHP?](https://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php) –  Sep 10 '17 at 02:22
  • @rtfm nope i don't want cron – Lil Zyz Sep 10 '17 at 02:27

1 Answers1

0

I'm not entirely sure this is what you mean, but if you're trying to make a script that is only executed if the current time is 00:xx:xx, you can easily check for that:

if (date('G') == 0) {
    // it is 00:00:00
} else {
    // it is 01:00:00 or later
}

However, if what you want is a script that somehow, automagically becomes active one time when it's midnight, you can put this in an infinite while loop:

set_time_limit(0);

$did_run = false;
while (true) {
    if (date('G') == 0) {
        // it is 00:00:xx (may not be exactly 00 seconds)
        if (!$did_run) {
            // the script hasn't run yet
            $did_run = true; // to make sure it doesn't run a second time

            // do stuff

        } else {
            // it is 00:00:xx but the script has already run
            sleep(30); // wait 30 seconds before checking again
        }
    } else {
        // it is 01:00:00 or later
        $did_run = false; // so it will run again the next day
        sleep(30); // wait 30 seconds before checking again
    }
}

Note that once you start this script, it will never end unless you manually kill it (or it crashes). That is why this is not a good solution, the better option would be to use a cron job (on Linux/Unix servers) or scheduled task (on Windows servers).

Just create your script file as you normally would. Do not put any date/time checking in the script itself, because you're going to let the operating system handle that.

Then for Linux, log into your server via SSH and run crontab -e, then add the following line:

0 0 * * * /usr/bin/php /path/to/my_script.php

This will tell the server to automatically run my_script.php at roughly 00:00:00 every day. For more information about setting up cron jobs, the Ubuntu Community page has some useful information.

If your hosting provider uses a control panel like DirectAdmin or CPanel or something, it's possible that control panel offers you this option and you don't need to log in via SSH.

For Windows servers, you can log into the server via Remote Desktop and configure a scheduled task via the Task Scheduler. I'm not that experienced with that, so I'll point you to the TechNet page that documents how to do that. The task will need to call php.exe with the full path to your script as an argument.

If you don't want to go to the trouble of setting this all up yourself, there are online solutions out there that call your script via a public URL every so often. One example is cron-job.org.

rickdenhaan
  • 10,857
  • 28
  • 37
  • you know what i mean – Lil Zyz Sep 10 '17 at 02:25
  • but i didn't understand your code can you explain how i put code in every 24 hour – Lil Zyz Sep 10 '17 at 02:25
  • 1
    Everything from "Just create your script as you normally would" is that explanation. What would you like me to clarify? – rickdenhaan Sep 10 '17 at 02:27
  • where should i put my code i want it active every 24H like i said – Lil Zyz Sep 10 '17 at 02:29
  • In a file. It doesn't matter where, as long as you use the correct file path in your crontab, scheduled task or the online service. For an online service, your script must be available via HTTP so the file must be somewhere in your document root. – rickdenhaan Sep 10 '17 at 02:31
  • you didn't understand me,, how i can know if time is 24H in your code like my question – Lil Zyz Sep 10 '17 at 02:34
  • That is done by the `if`-statement in my first example. `date('G')` returns the current hour according to the server (from 0 to 23) – rickdenhaan Sep 10 '17 at 02:35
  • i didjn't understand your answer so if i put my code in //do stuff it will reactive in every 24H – Lil Zyz Sep 10 '17 at 02:37
  • No. **Something** needs to *start* your script every 24 hours. *That* will be either a cron job, scheduled task or an online service. *Or* you can use my second example, then *you* start your script *once* and it will keep running *forever* and the script itself will need to make sure it only does something once every 24 hours. – rickdenhaan Sep 10 '17 at 02:41
  • i'm talking about second example btw, So if i put code in //doStuf it will work once every 24H? or not explain please – Lil Zyz Sep 10 '17 at 02:43
  • Yes. If you use that second example, you replace `// do stuff` with your code. – rickdenhaan Sep 10 '17 at 02:44