PHP date error

1

Hi, I'm doing an application with php codeigniter and sql server 2000. I try to make an insert to the bd and insert the data but the date you are saving is: 1894-07-07 00: 00: 00.0 (datetime) instead of the current date.

this is the code:

driver:

public function Save(){
        $fk_idInd = $this->input->post('txtIdIind');
        $fechaHora = date("m-d-Y");
        $hora = $this->input->post('txtHora');
        $estado = 1;
        $rut_res = '123456';
        $this->Indicaciones_model->insertControl($fk_idInd,$fechaHora,$estado,$rut_res);

    }

model:

public function insertControl($fk_idInd,$fechaHora,$estado,$rut_res){
        $sql = $this->db->query("INSERT INTO HCE_CONTROL_I_MED(FK_I_MED,FECHA_HORA,ESTADO,RUT_RES)
                                    VALUES($fk_idInd,$fechaHora,$estado,$rut_res)");
        return ($this->db->affected_rows() != 1) ? false : true;
    }

Any ideas of what is happening?

    
asked by daniel2017- 26.09.2017 в 16:29
source

2 answers

0

The default format to insert in the database is

$fechaHora = date("Y-m-d");

try to make a replace, sometimes it has happened that you do not insert the date correctly because the format goes wrong, try this function.

if (isset($_POST['txt_fecha'])) {
$apidate = $_POST['txt_fecha'];
$date = str_replace('/', '-', $apidate);
$fecha = date('Y-m-d', strtotime($date));

}

    
answered by 26.09.2017 / 16:52
source
1

When working with SQL Server database you should consider sending the date in ISO format, thus avoiding incompatibility with the date structure.

In your case you should consider:

$fechaHora = date("Ymd"); // 20170926

Or, if you want to consider the current date of the server where the database is, you should use GETDATE() :

$sql = $this->db->query("INSERT INTO HCE_CONTROL_I_MED(FK_I_MED,FECHA_HORA,ESTADO,RUT_RES) VALUES($fk_idInd, GETDATE(), $estado, $rut_res)");
answered by 26.09.2017 в 16:55