Is it recommended to handle date formatting directly in the database query using DATE_FORMAT in PHP to avoid date display errors?

When retrieving dates from a database in PHP, it is recommended to handle date formatting directly in the database query using DATE_FORMAT to avoid date display errors. This ensures that dates are formatted consistently and correctly displayed in your application.

// Example database query with date formatting using DATE_FORMAT
$query = "SELECT id, name, DATE_FORMAT(date_column, '%Y-%m-%d') as formatted_date FROM table_name";
$result = mysqli_query($connection, $query);

if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo $row['id'] . ' - ' . $row['name'] . ' - ' . $row['formatted_date'] . '<br>';
    }
}