function add_months($months, \DateTime $object)
{
$next = newDateTime($object->format('d-m-Y H:i:s'));
$next->modify('last day of +' . $months . ' month');
if ($object->format('d') > $next->format('d'))
{
return $object->diff($next);
}
else
{
returnnewDateInterval('P' . $months . 'M');
}
}
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
/**
* we'll need the element model for the radio button so we can get the
* label, as that isn't submitted with the form, it only exists as internal
* params of the element
*/
$elementModel = $formModel->getElement('00_calendar_entry___interval');
// $memberPlan is probablu going to be an array, as it's a radio
$memberPlan = $formModel->getElementData('00_calendar_entry___interval_raw');
$memberPlan = is_array($memberPlan) ? $memberPlan[0] : $memberPlan;
// Use the model getEmailValue() method to get the human readable label
$planLabel = $elementModel->getEmailValue($memberPlan, $formModel->formData);
// get the two dates, convert them to PHP DateTime objects
$activationDate = $formModel->getElementData('00_calendar_entry___start_date');
$activationDate = new DateTime($activationDate);
$billingDate = $formModel->getElementData('00_calendar_entry___start_date');
$billingDate = new DateTime($billingDate);
// start building the query ..
$myQuery->insert('00_calendar_inserts');
$myQuery->columns(
array(
$myDb->quoteName('start_date'),
$myDb->quoteName('interval'),
$myDb->quoteName('billing_date')
)
);
// loop around and insert the relevant number of times
for ($step = 1; $step <= 12; $step += $memberPlan) {
// insert the current value of $billingDate
$myQuery->values(
implode(',',
array(
$myDb->quote($activationDate->format('Y-m-d')),
$myDb->quote($planLabel),
$myDb->quote($billingDate->format('Y-m-d'))
)
)
);
// increment the plan date by the number of mnonths in the plan period
/*for ($i = 1; $i <= $memberPlan; $i++) {
$billingDate->modify('+1 month');
}*/
$billingDate->add(add_months($memberPlan, $billingDate));
}
// run the query
$myDb->setQuery($myQuery);
// uncomment this line if you need to debug
var_dump((string)$myQuery, $memberPlan, $activationDate, $billingDate);exit;
$myDb->execute();