How can PHP be used to format and store dates correctly in a MySQL database?

When storing dates in a MySQL database using PHP, it is important to format the date correctly to ensure it is stored accurately. One common way to do this is by using the `date()` function in PHP to format the date before inserting it into the database. Additionally, it is recommended to use the `DateTime` class in PHP to handle date and time manipulation to ensure accurate and consistent results.

// Create a new DateTime object with the current date and time
$date = new DateTime();

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

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

if ($result) {
    echo "Date stored successfully in the database.";
} else {
    echo "Error storing date in the database: " . mysqli_error($connection);
}