How to save user provided date in Mysql?

Asked

Viewed 3,803 times

4

I have a form which has a field where the user’s date of birth is entered, this field has a datepicker which already returns the date in year,month,day format, this date is accessed through $_GET['birthdate'] its format is string, then my question is how to insert it into the database? I have to assign this date to an intermediate object?

  • I think this has probably been answered in a number of different ways: http://answall.com/q/26968/101 http://answall.com/q/40262/101 http://answall.com/q/40731/101 http://answall.com/search?q=php+format+data

  • Hello bigown, I checked the links what worries me most is the format of the date that has this format: 1980/01/13, which modification should be made to insert it in mysql and how would do

  • This is one of the formats accepted by Mysql, are you experiencing a problem? http://dev.mysql.com/doc/refman/5.6/en/date-and-time-literals.html

  • should I switch to some special type of php or I can insert it directly into the string form?

1 answer

7


Would that be:

$date = date("Y-m-d", strtotime($_GET['birthdate'])); //converte para tipo date
mysqli_query($conexao, "INSERT INTO tabela (birthday) VALUES ($date)");

Behold working in the ideoneas far as possible. And in the repl it.. Also put on the Github for future reference.

Mysql documentation about dates.

Documentation of strtotime().

Documentation of date().

Note that the string date needs to be in a format minimally recognizable by PHP. Otherwise it will either fail or you will need to make a parse more complete to identify it.

  • It was this modification I was looking for

  • It is always good to check what the user is sending, before saving in the bank. I suggest a look at the answers of this question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.