How can PHP functions like date() and strtotime() be used to convert date formats for database insertion?

When inserting dates into a database, it is important to ensure that the date format is compatible with the database's requirements. PHP functions like date() and strtotime() can be used to convert date formats to the appropriate format for database insertion. The date() function can be used to format a date string, while the strtotime() function can convert a date string into a Unix timestamp.

// Example of converting a date format for database insertion
$dateString = "2022-01-15"; // Date string in 'YYYY-MM-DD' format
$timestamp = strtotime($dateString); // Convert date string to Unix timestamp
$formattedDate = date('Y-m-d H:i:s', $timestamp); // Format Unix timestamp to 'YYYY-MM-DD HH:MM:SS'

// Insert $formattedDate into the database
$query = "INSERT INTO table_name (date_column) VALUES ('$formattedDate')";
// Execute the query