[英]PHP: issuing a cron job once every 24hrs from initial date
I was wondering if I could get some help. 我想知道是否能得到一些帮助。
I have a cronjob that is run every 5 mins. 我有一个cronjob,每5分钟运行一次。
I also have a number of entries in my DB with a 'start date' formatted like 我的数据库中也有很多条目,其格式为“开始日期”
'yyyy-mm-dd HH:mm:ss'
What I want to do is run a specific command for each entry 24 hrs after the start date. 我想做的是在开始日期后24小时为每个条目运行一个特定的命令。
So for instance, if an entry has a start date of 2016-11-18 12:08:44
, I want to run a command on that entry at 2016-11-19 12:08:44 ...
Because the cron only runs every 5 mins, I don't expect to be exactly spot on 24hrs, but as close (5 mins each way) as possible. 因此,例如,如果某个条目的开始日期为2016-11-18 12:08:44
,我想在2016-11-19 12:08:44 ...
在该条目上运行命令2016-11-19 12:08:44 ...
因为仅cron每5分钟运行一次,我不希望在24小时内准确定位,但要尽可能地接近(每路5分钟)。
Is there any chance someone could give me an example of how I could achieve this in PHP? 有没有机会有人可以给我一个如何在PHP中实现此目标的示例?
Much appreciated. 非常感激。
You should do the job like this : 您应该这样做:
EDIT : 编辑:
Here a piece of source code, not tested ! 这里有一段源代码, 未经测试 !
$datetime = new \Datetime();
$timestamp = $datetime->getTimestamp();
$datetime_desired_launch = new \Datetime($your_formatted_datetime_from_db);
$desired_timestamp = $datetime_desired_launch->getTimestamp();
// I have manipulate timestamp for more efficiency
$launch_min_interval = $desired_timestamp - 5 * 3600; // - 5 minutes
$launch_max_interval = $desired_timestamp + 5 * 3600; // + 5 minutes
if ($timestamp >= $launch_min_interval && $timestamp <= $launch_max_interval) {
// Here launch action...
}
Be careful of timezones ! 注意时区!
Hope this help ! 希望对您有所帮助!
You can use crontab to lance a script each 5 min 您可以每隔5分钟使用crontab触发脚本
In this script you can check for date and lance the others scripts 在此脚本中,您可以检查日期并查看其他脚本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.