What is the best way to delete SQL data older than a certain date, such as 30 days, using PHP?

When deleting SQL data older than a certain date, such as 30 days, you can use a combination of PHP and SQL to achieve this. One way to do this is by using a SQL query with a WHERE clause that filters records based on their date. You can then execute this query using PHP to delete the desired data.

<?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 date threshold (e.g., 30 days ago)
$dateThreshold = date('Y-m-d', strtotime('-30 days'));

// SQL query to delete data older than the date threshold
$sql = "DELETE FROM your_table WHERE date_column < '$dateThreshold'";

// Execute the query
if ($conn->query($sql) === TRUE) {
    echo "Records deleted successfully";
} else {
    echo "Error deleting records: " . $conn->error;
}

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