在特定时间执行的 PHP 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1981036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:32:08  来源:igfitidea点击:

PHP script to execute at certain times

phptime

提问by meagar

Is there a simple way to have a php script execute some html at a certain times of the day?

有没有一种简单的方法可以让 php 脚本在一天中的某个时间执行一些 html?

For example i have on my home page a header and at certain times i want to be able to add something right under the header, in this case a iframe.

例如,我在我的主页上有一个标题,在某些时候我希望能够在标题下添加一些内容,在这种情况下是一个 iframe。

I know everyone mentioned cron jobs but how would this work with that? also is there an alternative? Its not available on all hosting

我知道每个人都提到了 cron 工作,但这将如何工作?还有其他选择吗?它并非在所有主机上都可用

回答by meagar

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.

cron 和 scheudled 作业的想法似乎与您实际尝试做的事情背道而驰。如果您只想在特定时间显示某些内容(在这种情况下为 iframe),您可以简单地检查每个请求期间的服务器时间,并选择在给定时间段内显示它。

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.

像这样的事情将产生与 cron 作业相同的效果,具有更多的粒度,在发出请求的确切时刻检查时间。

<!-- 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.

您可以扩展条件语句以每天多次显示 iframe,或者让您的脚本检查您希望用来管理 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.

使用服务器端 cron/任务调度程序作业定期从标题下方添加/删除 iframe 的想法比在每个请求期间简单地有条件地显示它要复杂得多。

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.

即使您有一些必须运行的特定任务,例如定期生成的报告,显示结果的实际工作通常也不会落在定期任务上。负责显示 iframe 的 PHP 脚本仍会在发出请求以显示任何新内容时查询数据库,并在找到时显示它,而不是定期任务以某种方式修改脚本以包含 iframe。

回答by jeffa00

If you are running on Linux, then you could have a cron job.

如果你在 Linux 上运行,那么你可以有一个 cron 工作。

If you are on Windows, then use Task Scheduler.

如果您使用的是 Windows,请使用任务计划程序。

If you are in a hosted environment, you need to check to see if either is allowed.

如果您在托管环境中,则需要检查是否允许其中任何一个。

回答by pix0r

cronon UNIX, launchdon OS X, and Task Scheduleron Windows platforms.

cron在 UNIX、launchdOS X 和Windows 平台上的任务计划程序上。

回答by sjobe

When you don't have access to cron jobs or scheduled tasks on your server, you can use online services such as http://pingability.com/to hit your script at specified intervals. It is not perfect, but you can build in some kind of secret key and code that makes sure that the script doesn't get run multiple times within a certain time period. Might seem a little hacky, but I've used it on live systems to send out daily emails and it's been working fine for over a year now.

当您无法访问服务器上的 cron 作业或计划任务时,您可以使用在线服务(例如http://pingability.com/)以指定的时间间隔点击您的脚本。它并不完美,但您可以构建某种密钥和代码,以确保脚本不会在特定时间段内多次运行。可能看起来有点笨拙,但我已经在实时系统上使用它来发送每日电子邮件,并且它已经运行了一年多。

回答by Darrell Brogdon

On Unix systems cronis your best bet.

在 Unix 系统上cron是你最好的选择。

回答by Wim

Use a cron job or something similar, see f.i. cron jobs or PHP scheduler

使用 cron 作业或类似的东西,请参阅 fi cron 作业或 PHP 调度程序

回答by Manjula

you can schedule the task as a cron job.

您可以将任务安排为 cron 作业。

回答by dekko

You could add a PHP script to your crontab to automatically run the script at defined intervals. From the command line, enter crontab -eto add the entry to your crontab.

您可以将 PHP 脚本添加到您的 crontab 以在定义的时间间隔自动运行脚本。在命令行中,输入crontab -e以将条目添加到您的 crontab。