What is the best practice for formatting and handling date inputs in PHP before inserting them into a database?

When handling date inputs in PHP before inserting them into a database, it is important to ensure that the date format is consistent and compatible with the database's date format. One common practice is to use PHP's date() function to format the date input before inserting it into the database. This helps to prevent any errors or inconsistencies that may arise from different date formats.

// Assuming $dateInput is the date input from a form
$date = date('Y-m-d', strtotime($dateInput));
// Now $date is formatted as 'YYYY-MM-DD' which is compatible with most database date fields

// Insert $date into the database using prepared statements or your preferred database interaction method