How can you delete entries from a database that are older than a certain year in PHP?

To delete entries from a database that are older than a certain year in PHP, you can use SQL queries with the DELETE statement combined with a WHERE clause that filters entries based on their date field. You can specify the condition to delete entries older than the desired year by comparing the date field with a specific date or year value.

<?php
// Connect to your 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);
}

// Define the year threshold
$year = 2020;

// SQL query to delete entries older than the specified year
$sql = "DELETE FROM your_table_name WHERE YEAR(date_field) < $year";

if ($conn->query($sql) === TRUE) {
    echo "Entries older than $year deleted successfully";
} else {
    echo "Error deleting entries: " . $conn->error;
}

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