Fix PHP date problem (CODEIGNITER)


In a controller I am sending 3 parameters to a model so that the boolean responds, and it always pulls out false, I have done tests so that the model returns what it is receiving and this is what it throws at me ..

["SE INGRESAN 10 NUEVOS PRODUCTOS AL STOCK ","01\/03\/2017","01\/05\/2017"]

How could I get the dates in a normal way?


public function insert_productos()
        $descripcion = 'SE INGRESAN 10 NUEVOS PRODUCTOS AL STOCK';//$this->input->post('descripcion');
        $fecha_inicial = "01/03/2017";//$this->input->post('fecha_inicial');
        $fecha_final = "01/05/2017";//$this->input->post('fecha_final');

        $response = $this->Periodo_model->insert_productos($descripcion,$fecha_inicial,$fecha_final);
        echo json_encode($response);


public function insert_productos($descripcion,$fecha_inicial,$fecha_final)
        $stmt = $this->db->conn_id->prepare("INSERT INTO 
        $result = $stmt->execute();
        return $result;


        selectMonths: true, // Creates a dropdown to control month
        selectYears: 15,// Creates a dropdown of 15 years to control year
        /*format: 'yyyy/mm/dd',*/
        /*formatSubmit: 'yyyy-mm-dd',*/
        format: 'dd/mm/yyyy',
        formatSubmit: 'dd/mm/yyyy',
        hiddenName: true,
        monthsFull: [ 'enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre' ],
        monthsShort: [ 'ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic' ],
        weekdaysFull: [ 'domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado' ],
        weekdaysShort: [ 'dom', 'lun', 'mar', 'mié', 'jue', 'vie', 'sáb' ],
        today: 'hoy',
        clear: 'borrar',
        close: 'cerrar',
        editable: true,
        min: 'today'

I would appreciate your help ...

asked by JDavid 01.03.2017 в 22:03

2 answers


try this:

     $descripcion = $this->input->post('descripcion');

     $("#fecha_inicial" ).datepicker( "option", "dateFormat", "dd/mm/yy" ); 
     $("#fecha_final" ).datepicker( "option", "dateFormat", "dd/mm/yy" ); 

     $fecha_inicial = $this->input->post('fecha_inicial');
     $fecha_final = $this->input->post('fecha_final');
answered by 01.03.2017 в 22:25

The INSERT command is most likely failing to interpret the dates correctly, but if you do not use the query method you do not take advantage of the CodeIgniter facilities to debug the query (method error ). It is also better to use the query method with the parameter assignment:

$result = $this->db->query(
    "INSERT INTO public.per_periodo(
    ) VALUES (?, ?, ?)",

if (!$result) {
    $error = $this->db->error(); // Arreglo con información de error

With this code you would be using a parameterized command that mitigates the risk of SQL code injection, letting CodeIgniter take care of the details and in case of error you have access to an arrangement with the information of it.

If you change the format of the date to aaaa-mm-dd , it should work without problems if the column in the database is of type DATE . In the database, columns of type DATE are not saved with format, but the format does matter when interpreting a string of characters as a date. And aaaa-mm-dd is the universal format that recognizes most conversions from a character string to date.

Now, you should always check the user's entries in all stages:

  • On the client / browser
  • In the application (model)
  • In the database

Therefore, the model should verify the data before sending it to the database. This includes checking the validity of the dates and that they are in the correct format.

answered by 02.03.2017 в 00:34