Convert PHP data to save in Mysql database [duplicate]

0

I need to receive a date using the datapicker as dd / mm / yyyy and switch to the accepted format in the database (mysql).

I have the accessor methods (Getter and Setter) in them I applied this function date and the strstring to convert:

function setDatavenc($datavenc) {
    $this->datavenc = date('d/m/Y', strtotime($datavenc));
}

.

function getDatavenc() {
    return date('d/m/Y', strtotime($this->datavenc));
}

But when saving to the bank saves 0000-00-00 And when doing the query returns: 01/01/1970 .

I've even looked for some form here in stackoverflow and none worked.

    
asked by anonymous 08.10.2018 / 17:29

1 answer

1

The PHP date function works as follows:

  

string date (string $ format [ int $ timestamp])

The format you can insert in the way you need it, using this TAB , from the PHP. In your case you need the format 0000-00-00 the code would look like this:

function setDatavenc($datavenc) {
    $this->datavenc = date('Y-m-d', strtotime($datavenc));
}
    
08.10.2018 / 17:35