我想知道如何安排动态(自动填充数据)功能在保存的时间每天自动运行?
假设我有一个表单,一旦单击该按钮,它就会将数据发送到函数,该函数会发布数据.我只想自动化,这样我就不必按下按钮了.
<ul>
<?php
foreach($Class->retrieveData as $data)
{
<form method="post" action="">
<li>
<input type="hidden" name="name">'.$data['name'].'<br/>
<input type="hidden" name="description">'.$data['description'].'<br/>
<input type="submit" name="post_data" value="Post">
</li>
</form>
}
?>
</ul>
现在,表单将数据传递给函数.
if(isset($_POST['post_data'])) // if post_data button is clicked then it runs myFunction()
{
myFunction();
}
myFunction()
{
$name = $_POST['name'];
$description = $_POST['description'];
}
我尝试了以下操作,但问题是Cron Job只能运行整个.php文件,而我正在检索从MySQL运行的已保存时间.
foreach($Class->getTime() as $timeData)
{
$timeHour = $timeData['timeHour'];
$timeMinute = $timeData['timeMinute'];
$hourMin = date('H:i');
$timeData = ''.$timeHour.':'.$timeMinute.'';
if($hourMin == $timeData)
{
run myFunction.
}
}
$hourMin是当前小时:分钟,它与从Mysql自动运行的已保存时间相匹配.因此,如果$hourMin == $timeData,那么该函数将运行.
如果$hourMin等于$timeData,如何运行Cron Job来自动运行myFunction()?
所以…
List 1 = is to be runned at 10am
List 2 = is to be runned at 12pm
List 3 = is to be runned at 2pm
上午10点,下午12点,下午2点是从MySQL检索但是基于每个列表ID的$timeHour和$timeMinute.
编辑
@randomSeed,
1) I can schedule cron jobs.
2) $name and $description will all be arrays, so the following is what I am trying to accomplish.
$name = array(
'Jon',
'Steven',
'Carter'
);
$description = array(
'Jon is a great person.',
'Steven has an outgoing character.',
'Carter is a horrible person.'
);
如果预定的时间是正确的,我想解析$name和$description中的第一个数组.
在数据库中,我有以下内容
postDataTime table
+----+---------+----------+------------+--------+
| iD | timeDay | timeHour | timeMinute | postiD |
+--------------------------------------+--------+
| 1 | * | 9 | 0 | 21 |
|----|---------|----------|------------|--------|
| 2 | * | 10 | 30 | 22 |
|----|---------|----------|------------|--------|
| 3 | * | 11 | 0 | 23 |
+----|---------+----------+------------+--------+
iD = auto incremented on upload.
timeDay = * is everyday (cron job style)
timeHour = Hour of the day to run the script
timeMinute = minute of the hour to run script
postiD = this is the id of the post that is located in another table (n+1 relationship)
如果难以理解.. what is quinoa
if(time() == 10:30(time from MySQL postiD = 22))
{
// run myFunction with the data that is retrieved for that time ex:
$postiD = '22';
$name = 'Steven';
$description = 'Steven has an outgoing character.';
// the above is what will be in the $_POST from the form and will be
// sent to the myFunction()
}
我只想根据保存到MySQL的时间安排所有内容,就像我在最顶层显示的那样(postDataTime表). (我会展示我曾经尝试过的东西,但是我已经搜索了无数个小时的例子来说明我想要完成的事情,但我找不到任何东西,我尝试的东西也不起作用.)
我以为我可以使用exec()函数,但从它看起来不允许我运行函数,否则我会做以下..
$time = '10:30';
if($time == time())
{
exec(myFunction());
}
解决方法:
你有2种方法,虽然只有一种方法可以完成你想做的事情;
>第一种方式要求您具有更改cron-jobs服务器端的访问权限和权限(例如通过PHP或其他方式).根据操作系统有什么教程:Win,Nix
>第二种方式将做一些接近你想要的东西,但没有分钟精度,你将在每个周期最多2分钟松动. (见下面的exaplanation).
第一路完美的方式
>只要用户点击表单,就会使用所需的数据时间为该用户创建一个唯一的cron-task.
if you don’t have those privileges you can use 3d part service like
07002 they also offer a Free version with limited
query. they also provide a 07003 method to manage (CRUDE)
cron-tasks.
第二种方式不完美的方式
>添加一个新的VARCHAR列,我今天用它来调用它,我们将确保该任务每天只运行一次.
–
+----+---------+----------+------------+--------+----------+
| iD | timeDay | timeHour | timeMinute | postiD | today |
+--------------------------------------+--------+----------+
| 1 | * | 9 | 0 | 21 | 30-05-04 |
|----|---------|----------|------------|--------|----------+
| 2 | * | 10 | 30 | 22 | |
|----|---------|----------|------------|--------|----------+
| 3 | * | 11 | 0 | 23 | |
+----|---------+----------+------------+--------+----------+
>之后创建一个我称之为crontask.php的php文件,我们将每隔5分钟调用一次
>将此添加到您的cronjob面板:
> 0,5 * * * * /usr/bin/php /www/virtual/username/crontask.php\u0026gt; / dev / null 2>& 1
>在crontask.php文件中
–
<?php
// include() Database Config file here with mysql_connect etc...
// include() the required files ex. the file where myFunction reside...
$cron_cycle = 5; // set it equal to what used in cron command-line
$today = date('Y-m-d');
if($result = mysql_query("SELECT * FROM postDataTime WHERE today != '{$today}'")){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$postID = $row['postID'];
$timeHour = (int) $row['timeHour'];
$current_hours = (int) date('H'); // current hours
$current_minutes = (int) date('i'); // current minutes
$timeMinute = (int) $row['timeMinute'];
// force to run at the closest cycle
$timeMinute = ($timeMinute % $cycle === 0) ? $timeMinute : toCloser($timeMinute, $cron_cycle);
if( $current_hours === $timeHour && $current_minutes === $timeMinute ){
// ensure that we have already runned a cron for this user...
mysql_query("UPDATE postDataTime SET today = '{$today}' WHERE postID = '{$postID}'");
myFunction($postID);
}
}
}
function toCloser($n,$x=5) {
$j = (round($n)%$x === 0) ? round($n) : (round(($n+$x/2)/$x)*$x);
return ($j-$n) >= round($x/2) ? ($j-$x) : $j;
}
?>
功能说明:
假设cron shedule每5分钟运行一次,lat表示我们在20:00,现在cron将在20:05,20:10,20:15,20:20运行,依此类推……
然后在我们的数据库中假设我们有那些时间
Jonh : 20:05,
Mario : 20:32,
luke : 20:48,
David : 20:57,
Jimmy : 20:06,
Eddy : 20:16
当脚本检查时,它将运行如下:
at 20:05 -> run 20:05 Jonh, 20:06 Jimmy
at 20:10 -> run null
at 20:15 -> run 20:16 Eddy
at 20:20 -> run null
and so on....
如你所见,你会在最糟糕的情况下每次2分钟松动.我认为这很公平!
标签:cron-task,php,mysql,cron 来源: https://codeday.me/bug/20191006/1861670.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。