What is the best practice for deleting entries from a database table after a certain time period in PHP?

To delete entries from a database table after a certain time period in PHP, you can use a combination of SQL queries and PHP code. One approach is to use a timestamp field in the database to track when each entry was created, and then periodically run a script that deletes entries older than a specified time period.

// Connect to the 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 time period (e.g. 30 days)
$timePeriod = strtotime('-30 days');

// Delete entries older than the time period
$sql = "DELETE FROM your_table WHERE created_at < $timePeriod";
if ($conn->query($sql) === TRUE) {
    echo "Entries older than 30 days have been deleted";
} else {
    echo "Error deleting entries: " . $conn->error;
}

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