There is a form with date input in the amount of 2 pieces (dtarrive, dtdepart) the format of these fields is dd-mm-yy because The database stores in the format yy-mm-dd must be converted to the format dd-mm-yy.

 INSERT INTO tbooking(dtarrive, dtdepart, userid) VALUES (LOWER(DATE_FORMAT("20-12-2017",'%d-%m-%Y')), LOWER(DATE_FORMAT(22-12-2017,"%d-%m-%Y")),"777") 

In this form and in various variations, the date is put 0000-00-00. Field in DB type DATE.

  • The database is stored in the format YYYY-MM-DD - Ipatiev
  • If one of the answers helped you, feel free to indicate that the answer helped you!) - hold me seven

2 answers 2

STR_TO_DATE () converts a string to the specified date values ​​based on an FMT format string. The STR_TO_DATE () function can return a date, time, or DATETIME value based on input and row format. If the input string is not valid, the STR_TO_DATE () function returns NULL.

 INSERT INTO tbooking SET dtarrive= STR_TO_DATE("20-12-2017", '%d-%m-%Y'), dtdepart= STR_TO_DATE("20-12-2017", '%d-%m-%Y'), userid = "777" 

Documentation is available here .

Field selection in database:

In your case, select Date if the date and time is DateTime. Documentation can read the link .

  • In this case, the input field should be of type text and not date, then you will need to implement the calendar in another way? - Den
  • No, no! This is for your example, on the view input, type = "date", and in the model, substitute the value from your input: dtarrive = STR_TO_DATE ('". $ ValueFromInput."', '% D-% m-% Y') - ultimatum
  • Everything works, I checked it myself, all that is left for you to do is take the value from the input and put it in the request. - ultimatum
  • I’ll come back from work, thanks! And another question in the database field set dtarrive dtdepart Varchar type? Or does the Date type understand this format? - Den
  • one
    Completed the answer. If the answer helped you, you can choose it as true!) - ultimatum

First, you do not need to write an arithmetic expression in the query instead of a date.
Secondly, the date has the format YYYY-MM-DD
Thirdly, the function STR_TO_DATE is used to convert formats in the muscle.
Fourth, you need to use prepared expressions to insert

 $sql = "INSERT INTO tbooking(dtarrive, dtdepart, userid) VALUES (STR_TO_DATE(?,'%d-%m-%y'), STR_TO_DATE(?,'%d-%m-%y'), ?)"; $pdo->prepare($sql)->execute(['12-12-16','12-01-17', 777]);