How can PHP developers effectively store and retrieve daily access statistics in text files, considering the need to differentiate between today's, yesterday's, and total accesses?

To effectively store and retrieve daily access statistics in text files, PHP developers can create separate text files for today's, yesterday's, and total accesses. They can use PHP functions like file_get_contents() and file_put_contents() to read from and write to these files. By maintaining distinct files for each day and a separate file for the total accesses, developers can easily differentiate between different time periods and retrieve the necessary statistics when needed.

// File paths for today's, yesterday's, and total access statistics
$today_file = 'access_stats_' . date('Y-m-d') . '.txt';
$yesterday_file = 'access_stats_' . date('Y-m-d', strtotime('-1 day')) . '.txt';
$total_file = 'total_access_stats.txt';

// Function to increment access count in a file
function incrementAccessCount($file) {
    $count = (int)file_get_contents($file);
    $count++;
    file_put_contents($file, $count);
}

// Increment today's access count
incrementAccessCount($today_file);

// Increment yesterday's access count if it exists
if (file_exists($yesterday_file)) {
    incrementAccessCount($yesterday_file);
}

// Increment total access count
incrementAccessCount($total_file);