Record data "date" in Mysql + PHP

0

I am trying to save a date in DD-MM-YYYY format, but I can not get it to be added in the bbdd. In PHP code I have:

$nid = $_POST['nid'];
$first_name = $_POST['first_name'];
$birthday = $_POST['birthday'];

$query = "INSERT INTO users('nid','first_name','birthday') VALUES('$nid','$first_name','$birthday')";

Is it possible with the query to define the format or previously with php do I have to change the format?

    
asked by Guif If 01.04.2018 в 19:08
source

3 answers

1

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.

    
answered by 01.04.2018 в 19:52
0

You can format it with php: link

There are many ways to do it, in your PHP file you can check if the variable comes empty or nulla but then you give it the format you want with the class, DateTime () and store it, you have to take into account the type of field in the database. (date, datetime, timestamp) since each one has a different format

    
answered by 01.04.2018 в 19:24
0

Although for the data type that is date () and that reads in the form of YYYY-MM-DD; you can make the user read it in another way, if you use the DateTime class as follows

$fecha = new DateTime('2018-04-02');
echo $fecha->format('d-m-Y');
  

Where date you can adjust it so that for example if it is the data that   comes from the database so you format it to the user and   you already read it in the structure you want

This way you do not affect the format in which you save but you would only have to make the adjustments to implement in your frontend in this way; it's an option

    
answered by 01.04.2018 в 19:20