What are some common pitfalls when using DATE_SUB function in PHP to delete entries from a MySQL database?

One common pitfall when using the DATE_SUB function in PHP to delete entries from a MySQL database is not properly formatting the date in the correct MySQL date format. To solve this issue, make sure to format the date using the date() function with the 'Y-m-d' format before passing it to the DATE_SUB function.

// Correct way to delete entries using DATE_SUB function
$date = date('Y-m-d', strtotime('-7 days'));
$query = "DELETE FROM table_name WHERE date_column < DATE_SUB(NOW(), INTERVAL 7 DAY)";
$result = mysqli_query($connection, $query);
if($result){
    echo "Entries deleted successfully";
} else {
    echo "Error deleting entries: " . mysqli_error($connection);
}