Aman Mishra
Aman Mishra

Reputation: 86

How to add a month to a date from form input field

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);
          }

Upvotes: 0

Views: 218

Answers (2)

Atural
Atural

Reputation: 5439

Thats a pretty straight forwarded issue - you can perfectly solve this by using the DateTime Class which is part of PHP since Version 5.2.

Something like the followinig should do the job

$strDate = $this->input->post('date');
$objDate = DateTime::createFromFormat('Y/m/d', $strDate);

for($i = 1; $i <= 12; $i++)
{
    $maintenance_data = array(
        'site_id' => $this->input->post('site_id'),
        'date' => $objDate->format('Y-m-d')
    );

    $this->Maintenance_Model->add_maintenance($maintenance_data);
    $objDate->add(new DateInterval('P1M'));
}

Upvotes: 1

Umer Abbas
Umer Abbas

Reputation: 1876

The problem is with your for loop when it runs first time it has correct format for $main_date but the second time it's format is changed that's why it's not working for the next 11 times. Also you would want to use $main_date in the following line in your for loop.

$main_date = strtotime(date("Y-m-d", strtotime($date)) . "+1 month");

I want to take this moment to explain that this solution will bring problems to you, for example take a look at this question previously asked.

So I would like to suggest using a library Carbon for calculating dates and time

Upvotes: 1

Related Questions