How can the MySQL function DATE_SUB() be used to check for entries older than a specific number of days in PHP?

To check for entries older than a specific number of days in MySQL using the DATE_SUB() function, you can subtract the desired number of days from the current date and compare it with the date column in your database table. This can be achieved by constructing a SQL query that utilizes the DATE_SUB() function with the appropriate parameters.

<?php
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Define the number of days
$days = 30;

// Construct SQL query to select entries older than $days days
$query = "SELECT * FROM table_name WHERE date_column < DATE_SUB(NOW(), INTERVAL $days DAY)";

// Execute the query
$result = $mysqli->query($query);

// Fetch and display results
while ($row = $result->fetch_assoc()) {
    echo $row['column_name'] . "<br>";
}

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