How can the MySQL function DATE_FORMAT be utilized in PHP to format dates retrieved from the database?

When retrieving dates from a MySQL database in PHP, you may want to format them in a specific way before displaying them to the user. This can be done using the DATE_FORMAT function in MySQL to format the dates directly in the SQL query. By using DATE_FORMAT, you can specify the desired format for the date output, such as 'Y-m-d' for year-month-day.

// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Query to retrieve dates from the database and format them using DATE_FORMAT
$query = "SELECT DATE_FORMAT(date_column, '%Y-%m-%d') AS formatted_date FROM table_name";

$result = $mysqli->query($query);

// Loop through the results and display the formatted dates
while ($row = $result->fetch_assoc()) {
    echo $row['formatted_date'] . "<br>";
}

// Close the database connection
$mysqli->close();