How can PHP handle date and time formats when inserting data into a MySQL database?

When inserting date and time data into a MySQL database using PHP, it's important to format the date and time correctly to ensure it is stored accurately in the database. One way to handle this is by using the `date()` function in PHP to format the date and time according to the MySQL datetime format (`Y-m-d H:i:s`). This ensures that the data is inserted correctly into the database.

// Get the current date and time
$currentDateTime = date('Y-m-d H:i:s');

// Insert data into MySQL database with the formatted date and time
$query = "INSERT INTO table_name (date_time_column) VALUES ('$currentDateTime')";
$result = mysqli_query($connection, $query);

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