In what situations would it be more efficient to handle date formatting directly in a database query rather than in PHP code?
When dealing with large datasets or complex queries, it may be more efficient to handle date formatting directly in a database query rather than in PHP code. This can help reduce the amount of data transferred between the database and the PHP application, improving performance and reducing the workload on the PHP server.
// Example of handling date formatting directly in a MySQL query
$query = "SELECT id, name, DATE_FORMAT(created_at, '%Y-%m-%d') AS formatted_date FROM users";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['id'] . ' - ' . $row['name'] . ' - ' . $row['formatted_date'] . '<br>';
}