How can date and time values be properly formatted and stored in a MySQL database using PHP to avoid errors like failed auto-increment value retrieval?

When storing date and time values in a MySQL database using PHP, it is important to properly format the values to avoid errors like failed auto-increment value retrieval. One way to ensure this is by using the MySQL DATETIME format ('Y-m-d H:i:s') when inserting date and time values into the database. Additionally, make sure to use the proper functions like date() in PHP to format the values correctly before inserting them into the database.

// Assuming $date is the date value to be inserted into the database
$date = date('Y-m-d H:i:s', strtotime($date));

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Insert formatted date value into database
$query = "INSERT INTO table_name (date_column) VALUES ('$date')";
$mysqli->query($query);

// Close database connection
$mysqli->close();