There are several things to keep in mind:
- The user is entering the data in this format:
DD-MM-YYYY
- You need to insert that data into the database in a column of type
DATE
or DATETIME
- The database manages these fields with the format
YYYY-MM-DD
... and so it must remain.
In this case, your duty as a programmer is to feed the database as it works internally. To do this, you must take the user's input and convert it to a valid format for the database .
This you can do in several ways. The most appropriate would be to use the class DateTime
, using the method createFromFormat
.
For example:
$fecha = DateTime::createFromFormat('d-m-Y', $birthday)->format('Y-m-d');
What is done here is to take the user's entry, saved in $birthday
, knowing that said entry comes in d-m-Y
format to create a DateTime
object and get the variable $fecha
a representation of that object in a valid format to be entered in the database.
If the user's entry retrieved in $birthday
is for example: 01-04-2018
, the previous code will throw this in the variable $fecha
: 2018-04-01
. That will be the value that you will enter in the database:
$query = "INSERT INTO users(nid,first_name,birthday) VALUES('$nid','$first_name','$fecha')";
Note that here we are using the variable $fecha
and not the variable $birthday
.
PS: In the future, consider giving security to your query, since the
It is vulnerable to SQL injection attacks.