daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Oct 12th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. require_once './vendor/autoload.php';
  4.  
  5. /** @todo read straight from the crontab file */
  6. $jobs = [
  7.     'task1' => [
  8.         '0 6 * * *'           => (new \Cron\CronExpression('0 6 * * *')),
  9.         '45 7 * * *'          => (new \Cron\CronExpression('45 7 * * *')),
  10.         '15 8 * * *'          => (new \Cron\CronExpression('15 8 * * *')),
  11.         '30 8 * * *'          => (new \Cron\CronExpression('30 8 * * *')),
  12.         '45 8 * * *'          => (new \Cron\CronExpression('45 8 * * *')),
  13.         '00 8,12,15,18 * * *' => (new \Cron\CronExpression('00 8,12,15,18 * * *')),
  14.         '30 12 * * *'         => (new \Cron\CronExpression('30 12 * * *')),
  15.        
  16.     ],
  17.     'task2' => [
  18.         '*/20 9,10,11,13,14,16,17,19,20 * * *' => (new \Cron\CronExpression('*/20 9,10,11,13,14,16,17,19,20 * * *')),
  19.         '20,40 12,15,18 * * *'                 => (new \Cron\CronExpression('20,40 12,15,18 * * *')),
  20.     ],
  21.     'task3' => [
  22.         '18 8-18/4 * * *' => (new \Cron\CronExpression('18 8-18/4 * * *')),
  23.     ],
  24.     'task4' => [
  25.         '35 19 * * *' => (new \Cron\CronExpression('35 19 * * *')),
  26.     ],
  27.     'task5' => [
  28.         '10,30,50 9,10,11,13,14,15,17,18,19 * * *' => (new \Cron\CronExpression('10,30,50 9,10,11,13,14,15,17,18,19 * * *')),
  29.         '10,30 16,20 * * *'                        => (new \Cron\CronExpression('10,30 16,20 * * *')),
  30.         '10,35 12 * * *'                           => (new \Cron\CronExpression('10,35 12 * * *')),
  31.     ],
  32.     'task6' => [
  33.         '50 8,12,16,20 * * *' => (new \Cron\CronExpression('50 8,12,16,20 * * *')),
  34.     ]
  35. ];
  36.  
  37. $times = [];
  38.  
  39. /** Create a period of minutes for a full day */
  40. $begin = (new DateTime())->setTimezone(new DateTimeZone('Europe/London'))->setTime(0, 0, 0);
  41. $end = (new DateTime())->setTimezone(new DateTimeZone('Europe/London'))->setTime(23, 59, 59);
  42. $interval = DateInterval::createFromDateString('1 minute');
  43. $periods = new DatePeriod($begin, $interval, $end);
  44.  
  45. foreach ($jobs as $jobName => $jobTimes) {
  46.  
  47.     \array_walk($jobTimes, function($jobTime, $jobIndex) use ($periods, $jobName, &$times) {
  48.        
  49.         $previousNextJobTime = false;
  50.        
  51.         foreach ($periods as $period) {
  52.            
  53.             $period = $period->setTimezone(new DateTimeZone('Europe/London'));
  54.            
  55.             /* @var $period DateTime */
  56.             /* @var $jobTime \Cron\CronExpression */
  57.            
  58.             $currentNextJobTime = (new \DateTime("@" . $jobTime->getNext($period)))->setTimezone(new DateTimeZone('Europe/London'));
  59.  
  60.             /** Skip periods between current period selection and the last next job calculated to run */
  61.             if($previousNextJobTime && $period < $previousNextJobTime) {
  62.                 continue;
  63.             }
  64.            
  65.             /** Skip previous next job time and current next job time if they are the same calulation */
  66.             if($previousNextJobTime && $previousNextJobTime == $currentNextJobTime) {
  67.                 continue;
  68.             }
  69.            
  70.             $endOfDayCutoff = (new DateTime())->setTime(23, 59, 59)->setTimezone(new DateTimeZone('Europe/London'));
  71.  
  72.             /** ignore if we've gone over the cutoff time */
  73.             if($currentNextJobTime > $endOfDayCutoff) {
  74.                 continue;
  75.             }
  76.            
  77.             $times[$jobName][$jobIndex] = $currentNextJobTime->format('Y-m-d H:i:s');
  78.            
  79.             echo $jobName . '|' . $jobIndex . '|' . $currentNextJobTime->format('Y-m-d H:i:s') . PHP_EOL;
  80.             $previousNextJobTime = $currentNextJobTime;
  81.            
  82.         }
  83.         reset($periods);
  84.     });
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top