K.Wayne K.Wayne - 6 months ago 12
SQL Question

How I can insert birth date in MySQL with php?

I'm trying to insert a birth date in MySQL with php, but it displays

00-00-0000
,
In html I have three dropdowns with Month, Date and Year, and in MySQL the column is
name: birthdate
,
Type: DATE
. I'm trying to insert with the below PHP script, but I can not.

Please see the script below:

if (isset($_POST['first']) && isset($_POST['last']) && isset($_POST['email']) && isset($_POST['password'])) {
if (!empty($_POST['first']) && !empty($_POST['last']) && !empty($_POST['password'])) {
$user = new User();
$user - > id['id'];
$user - > firstname = $_POST['first'];
$user - > lastname = $_POST['last'];
$user - > email = $_POST['email'];
$user - > password = $_POST['password'];
$month = $_POST['month'];
$dt = $_POST['dt'];
$year = $_POST['year'];
$user - > birthdate = $date_value = "$month/$dt/$year";
if ($user - > Create()) {
header("Location:profile.php");
} else {
echo "<a>User already exist!</a>";
}
}
}

Answer

Try your birthdate like below

 $month = date('m', strtotime($month));
   /* or you can directly covert like this also $month = date('m', strtotime($_POST['month'])); */
    $date_value = $year. "-" .$month . "-".$dt;
    $user->birthdate = date('Y-m-d',strtotime($date_value));

Here i have convert your date in Y-m-d format as MySQL date datatype accept this format You can find more about strtotime on this link