What is the correct format for the date when inserting into a database in PHP?

When inserting a date into a database in PHP, it is important to format the date correctly to ensure it is stored accurately. The most common and recommended format for dates in databases is the YYYY-MM-DD format. This format is easily understood by databases and ensures consistency in date storage.

$date = "2022-12-31"; // Date to be inserted into the database
$formatted_date = date('Y-m-d', strtotime($date)); // Format the date to YYYY-MM-DD format

// Insert the formatted date into the database
$query = "INSERT INTO table_name (date_column) VALUES ('$formatted_date')";
// Execute the query using your preferred method (e.g. mysqli_query, PDO, etc.)