How can PHP be used to filter and separate old entries from new entries in a CSV file for archiving purposes?

To filter and separate old entries from new entries in a CSV file for archiving purposes, you can compare the date of each entry with a specified threshold date. Entries older than the threshold date can be moved to a separate archive file while newer entries can be kept in the original file.

<?php
// Specify the threshold date for archiving old entries
$thresholdDate = strtotime('2021-01-01');

// Open the original CSV file for reading
$originalFile = fopen('original.csv', 'r');

// Open a new archive CSV file for writing
$archiveFile = fopen('archive.csv', 'w');

// Loop through each entry in the original file
while (($data = fgetcsv($originalFile)) !== false) {
    // Check if the entry date is older than the threshold date
    if (strtotime($data[0]) < $thresholdDate) {
        // Write the old entry to the archive file
        fputcsv($archiveFile, $data);
    } else {
        // Write the new entry back to the original file
        fputcsv($originalFile, $data);
    }
}

// Close both files
fclose($originalFile);
fclose($archiveFile);
?>