How can you ensure that the date format in PHP matches the format expected by MySQL in a query?

When passing dates from PHP to MySQL in a query, it is important to ensure that the date format matches the format expected by MySQL, which is 'YYYY-MM-DD'. To achieve this, you can use PHP's date() function to format the date accordingly before including it in the query.

// Assuming $date contains the date value in a different format
$formatted_date = date('Y-m-d', strtotime($date));

// Now you can use $formatted_date in your MySQL query
$query = "INSERT INTO table_name (date_column) VALUES ('$formatted_date')";