The date after the selection in input has the form December 31, 2016, how can I write it in the database, if there I need it to be in Ymd format?

 $termin = $product['date_available']; //здесь я получаю дату в формате dd.mm.yyyy $this->data['date_available'] = date("DmY", strtotime($termin)); //сюда нужно передать дату в формате yyyy-mm-dd 
  • why D and not d ? and the date format in the database is usually yyyy-mm-dd, i.e. Ymd - Alex
  • so I was advised in the answer below - Abmin
  • in the answer below, too, D , not d - it will output 2016-10-Sat , and you need 2016-10-29 , i.e. Ymd - Alex
  • Forwarded D to d, the result is the same. Date was 10/29/2016 and 2029-10-20 entered the database - Abmin
  • Please see the answer , you have the wrong date inserted, because You specify an incorrect date format. DmY to Ymd . those. You are trying to insert the date 29-10-2016 , but you need 2016-10-29 . - Alex

3 answers 3

Option 1 - bring the date to the format before writing to the database.

 $this->data['date_available'] = date("DmY", strtotime($termin)); 

change to

 $this->data['date_available'] = date("Ymd", strtotime($termin)); 

Option 2 - lead to the desired format in the request to the database:

 STR_TO_DATE('29.10.2016','%d.%m.%Y') 

    $date = "31.12.2016"; echo date("YmD", strtotime($date));

    This is how you can reformat the date.

    • I have forwarded the question using your answer, but it doesn't work that way. Date was 10/29/2016 and 2029-10-20 entered the database - Abmin

    It seems to me that things are better for using the same MySQL tools.

     INSERT INTO table_name VALUES ('', STR_TO_DATE( '31.12.2016', '%d.%m.%Y' )) 
    • Answers do not look at all) - Alex
    • Well, while I was writing my own and just in case checking its performance in a real database, yours appeared :) - Venta