What is the best practice for inserting dates into a MySQL database using PHP?

When inserting dates into a MySQL database using PHP, it is best practice to use the `DateTime` class to format the date in the desired format before inserting it into the database. This ensures that the date is formatted correctly and avoids any potential SQL injection vulnerabilities.

// Create a new DateTime object with the date value
$date = new DateTime($dateString);

// Format the date in the desired format
$formattedDate = $date->format('Y-m-d H:i:s');

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

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