0

I have a text field where you can enter a date in the format dd/mm/yyyy, and I want to convert that date as yyyy-mm-dd to store in my database.

I know this has been asked many times before, and a common answer is trying something like this.

$var = '20/04/2012';
$date = str_replace('/', '-', $var);
echo date('Y-m-d', strtotime($date));

The problem is I do not know how to apply that to my situation.

This is the code I have in my view

<input type="text" name="initialDate" value="<?php echo $this->input->post('fechaElaboracion'); ?>" class="form-control" id="initialDate" />

In my controller I get all the data from the input fields in my form, save them in an array and then call a function in my model to store them in the database.

$params = array(
    'name' => $this->input->post('name'),
    'address' => $this->input->post('address'),
    'initialDate' => $this->input->post('initialDate'),
);
$po_general_id = $this->Pogeneralmodel->add_po_general($params);
redirect('po_general/index');

Do I need to reformat the date in the view or in the controller? How can I do it? Thanks.

raptorandy
  • 225
  • 5
  • 21
  • `$date = new DateTime($dateFromInput); echo $date->format("your format")` use something like that – kRicha Jun 08 '17 at 19:59

1 Answers1

1

Your view is only to display the results came from your Controller. In this case, it could be done in your Controller before passing to Model.

$newDate = date("Y-m-d",strtotime($this->input->post('initialDate')));
$params = array(
    'name' => $this->input->post('name'),
    'address' => $this->input->post('address'),
    'initialDate' => $newDate,
);
$po_general_id = $this->Pogeneralmodel->add_po_general($params);
redirect('po_general/index');