简体   繁体   English

PHP:从初始日期起每24小时发布一次Cron作业

[英]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 : 您应该这样做:

  1. Add a column "desired launch date" in the DB (Nullable). 在数据库中添加一列“所需的启动日期”(可空)。
  2. Write a php script what calculate all 'desired launch date' from 'start_date' (and update the DB with calculed date). 编写一个php脚本,根据“ start_date”计算所有“所需的启动日期”(并使用计算出的日期更新数据库)。
  3. Add block on this PHP script that initialize current datetime and compare with 'desired launch date' with an interval of 10 minutes. 在此PHP脚本上添加一个块,用于初始化当前日期时间,并与“期望的启动日期”进行比较,间隔为10分钟。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM