How can PHP prepared statements be used to format dates retrieved from a database using DATE_FORMAT()?

When retrieving dates from a database in PHP, you can use prepared statements along with the DATE_FORMAT() function to format the dates as needed. By using prepared statements, you can ensure the security of your database queries and prevent SQL injection attacks. To format dates retrieved from a database using DATE_FORMAT(), you can simply include the DATE_FORMAT() function in your SQL query and bind the retrieved date value to a variable in your PHP code.

// Assuming $pdo is your database connection
$stmt = $pdo->prepare("SELECT DATE_FORMAT(date_column, '%Y-%m-%d') AS formatted_date FROM your_table WHERE conditions");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

$formattedDate = $result['formatted_date'];
echo $formattedDate;