What are some common pitfalls when trying to insert a Date value into a MySQL database using PHP?

One common pitfall when inserting a Date value into a MySQL database using PHP is not formatting the date correctly. MySQL expects dates in the 'YYYY-MM-DD' format, so it's important to convert any date values to this format before inserting them into the database. Another common mistake is not properly escaping the date value, which can lead to SQL injection vulnerabilities.

// Assuming $date contains the date value to be inserted
$date = date('Y-m-d', strtotime($date));

// Assuming $conn is the MySQL database connection
$query = "INSERT INTO table_name (date_column) VALUES ('$date')";
$result = mysqli_query($conn, $query);

if($result){
    echo "Date inserted successfully";
} else {
    echo "Error inserting date: " . mysqli_error($conn);
}