I am trying to add 12 maintenance reminders when one date is entered through a form field. Each of these maintenance reminders are on the same date of each month. After running some basic validation I have initiated a for loop to do this. The problem is : the first reminder date is perfect but al the 11 dates are 0000-00-00 . I tried looking at solutions online but couldn't solve this.
$this->load->model('Site_Model');
$this->load->model('Maintenance_Model');
$data['sites'] = $this->Site_Model->view_all_sites();
if($this->input->post('save'))
{
$this->form_validation->set_rules('site_id', 'Site ID', 'trim|required');
$this->form_validation->set_rules('date', 'Maintenance Date', 'trim|required');
$var = $this->input->post('date');
$date = str_replace('/', '-', $var);
$main_date = date('Y-m-d', strtotime($date));
if ($this->form_validation->run()){
for ($x = 1; $x <= 12; $x++) {
$maintenance_data = array(
'site_id' => $this->input->post('site_id'),
'date' => $main_date
);
$this->Maintenance_Model->add_maintenance($maintenance_data);
$main_date = strtotime(date("Y-m-d", strtotime($date)) . "+1 month");
}
$this->session->set_flashdata('success','Maintenance Scheduled Successfully');
redirect('dashboard/add_maintenance');
}else{
$this->session->set_flashdata('failure', validation_errors());
redirect('dashboard/add_maintenance');
}
}else {
$this->load->view('templates/header' , $data);
$this->load->view('dashboard/add_maintenance' , $data);
$this->load->view('templates/footer' , $data);
}