What are the best practices for inserting dates into a MySQL database using PHP to ensure accurate data storage?

When inserting dates into a MySQL database using PHP, it is important to ensure that the date format is compatible with MySQL's date format (YYYY-MM-DD). To ensure accurate data storage, it is recommended to use PHP's date() function to format the date before inserting it into the database.

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

// Insert the formatted date into the database
$query = "INSERT INTO table_name (date_column) VALUES ('$formatted_date')";
$result = mysqli_query($connection, $query);

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