Add recurring task php


<?php

$startDate = $postData[‘start_date’];
$endDate = $postData[‘end_date’];
$endDate = date(“Y-m-d”, strtotime($endDate . ” +1 day”));

/* For weekly schedule
* frequency_id:
*      ‘1’ => ‘Daily’,
*      ‘2’ => ‘alternative day’,
*      —-Every——–
*      ‘3’ => ‘Monday’,
*      ‘4’ => ‘Tuesday’,
*      ‘5’ => ‘Wednesday’,
*      ‘6’ => ‘Thursday’,
*      ‘7’ => ‘Friday’,
*      ‘8’ => ‘Saturday’,
*      ‘9’ => ‘Sunday’
*  */
if (in_array ($postData[‘frequency_id’], array(3,4,5,6,7,8,9))) {
$beginDate = new DateTime($startDate);
$endDate1 = new DateTime($endDate);

$interval = DateInterval::createFromDateString(‘1 day’);
$period = new DatePeriod($beginDate, $interval, $endDate1);

$arrDay = array(‘3’ => ‘Monday’, ‘4’ => ‘Tuesday’, ‘5’ => ‘Wednesday’, ‘6’ => ‘Thursday’, ‘7’ => ‘Friday’, ‘8’ => ‘Saturday’, ‘9’ => ‘Sunday’);

foreach ($period as $dt)
if ($dt->format(“l”) == $arrDay[$postData[‘frequency_id’]]) {
$startDate = $dt->format(“Y-m-d”);
break;
}
}

$begin = new DateTime($startDate);
$end = new DateTime($endDate);

if ($postData[‘frequency_id’] == ‘1’) { // daily
$interval = DateInterval::createFromDateString(‘1 day’);
} elseif ($postData[‘frequency_id’] == ‘2’) { // alternative day
$interval = DateInterval::createFromDateString(‘2 day’);
} elseif (in_array ($postData[‘frequency_id’], array(3,4,5,6,7,8,9))) {
$interval = DateInterval::createFromDateString(‘7 day’);
}

$period = new DatePeriod($begin, $interval, $end);

foreach ($period as $dt) {
$postData[‘frequency_post_id’] = $frequencyPostAdd;
$postData[‘post_date’] = $dt->format(“Y-m-d”);

/********** Your Logik *************/
}

Advertisements