How can the DATE function in MySQL be used to filter data based on specific days in PHP?

To filter data based on specific days using the DATE function in MySQL in PHP, you can use the DATE function in your SQL query to extract the date part of a datetime column and then compare it with the desired date. This allows you to retrieve records that match the specified day.

<?php

// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Specify the desired day
$desired_day = "2022-01-01";

// SQL query to filter data based on specific day
$sql = "SELECT * FROM table_name WHERE DATE(date_column) = '$desired_day'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // Output data
        echo "ID: " . $row["id"]. " - Date: " . $row["date_column"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

?>