How can I make my PHP script run at a certain time everyday?
What you want is called a cron job and is already covered here:
PHP: running scheduled jobs (cron jobs)
PHP script to execute at certain times
The idea of cron and scheudled jobs seems to run counter to what you're actually trying to do. If you want something to display (an iframe in this case) only during certain times, you can simply check the server time during each request, and opt to display it if you're within a given time period.
Something like this will produce the same effect as a cron job, with more granularity, checking the time at the exact moment the requst is made.
<!-- Your Header here -->
<?php
$hour = date('G'); // 0 .. 23
// Show our iframe between 9am and 5pm
if ($hour >= 9 && $hour <= 17) { ?>
<iframe .... ></iframe>
<?php } ?>
You can expand on the conditional statement to show the iframe multiple times per day, or have your script check whatever external condition you're looking to use to govern the display of your iframe.
Update:
Additional times or types of comparisons could be specified via something like
<?php
$hour = date('G');
$day = date('N'); // 1..7 for Monday to Sunday
if (($hour >= 5 && $hour <= 7) // 5am - 7am
|| ($hour >= 10 && $hour <= 12) // 10am - 12 noon
|| ($hour >= 15 && $hour <= 19) // 3pm - 7pm
|| ($day == 5) // Friday
) { ?>
<iframe...></iframe>
<?php } ?>
The idea of periodically adding/removing the iframe from below your header with a server-side cron/task scheduler job is far more complex than simply conditionally displaying it during each request.
Even if you have some specific task which must run, such as a periodically generated report, the actual job of displaying the results usually don't fall upon the periodic task. The PHP script responsible for showing that iframe would still query the database at the time the request is made for any new content to show, and display it if found, rather than the periodic task somehow modifying the script to include an iframe.
What options are there for executing a PHP script at a certain time every day?
Depends how exact the timing needs to be. A technique I've seen used (dubbed "poor man's cron") is to have a frequently accessed script (a site's home page, for example) fire off a task on the first page load after a certain time (kept track of in the database).
If you need any sort of guaranteed accuracy, though, cron or a Windows scheduled task is really the best option. If your host doesn't support them, it's time to get a better one.
Run code only between certain hours
if(date("Hi") < 2200 && date("Hi") > 800){
// Run Code
}
automatically schedule a php script execution at specific time
You have 3 options. My recommendation: Use cron if you can, user driven if you have to, and daemon as a last resort.
(1) cron (as mentioned in comments)
cron is a scheduler for linux systems that will run a command line job on your system. You log into your server over ssh, type crontab -e
, and add a line like this:
4 5 * * * php /path/to/my/script.php
This would run the script at 5:04 a.m. every day.
<?php
// /path/to/my/script.php
// Do something
Some hosting services allow entering cron jobs with a GUI. There are also external cron services, that will call a URL for you at specific times.
(2) daemon
This is the most advanced option and also the least reliable: You run a command line script that contains an infinite loop. The script then periodically checks state and responds to it. Because it is likely to crash after months of running, you have to have a second script to restart it in case it does. This is a lot of work, but it is the most flexible approach.
<?php
while (1) {
// fetch $last_exec_timestamp from database
if ($last_exec_timestamp < time() + 86400) {
// set last_exec_timestamp to now in database
// do something
}
sleep(5);
}
3. user driven
If you have a decent amount of traffic on your site, you can simply include a the job this in your page footer, when there is no more output. Make sure this code is fast, or an unlucky user will be waiting for it.
<?php
// fetch $last_exec_timestamp from database
if ($last_exec_timestamp < time() + 86400) {
// set last_exec_timestamp to now in database
// do something
}
There are also to more fancy approaches of "user driven" that I haven't personally tested in another stack overflow question.
Executing PHP code after a specific time
You'll need to route it back round to your server, but this asynchronous php approach works quite nicely (I've been using it for about a year to do almost exactly what you seem to be doing).
See: How to run the PHP code asynchronous
Related Topics
"Usort" a Doctrine\Common\Collections\Arraycollection
Log the Actual SQL Query Using Activerecord with Yii2
PHP Copying Array Elements by Value, Not by Reference
Using Return in Ternary Operator
Php's Preg_Replace Regex That Matches Multiple Lines
Differencebetween Redirect and Forward in Zend Framework
How to Do a Curl Request to the Same Server
How to Convert Array of Bytes to a String in PHP
Differencebetween Switch-Case and If-Else in PHP
Multiple Http Get Parameters with the Same Identifier
What Does Bind_Param Accomplish
How to Create a Database-Driven Multi-Level Navigation Menu Using Laravel
Create Programmatically a Product Using Crud Methods in Woocommerce 3
Get the Index of a Certain Value in an Array in PHP
PHP Reflection - Get Method Parameter Type as String
How to Increment Count in the Replacement String When Using Preg_Replace