How can the current time be stored and checked when incrementing a counter in PHP to avoid the need for a daily reset script?

When incrementing a counter in PHP, the current time can be stored in a database or a file along with the counter value. This way, the script can check the stored time against the current time to determine if a day has passed since the last increment. If a day has passed, the counter can be reset to 0.

// Store the counter and last updated time in a file
$counterFile = 'counter.txt';
$counterData = file_get_contents($counterFile);
$counterArray = explode('|', $counterData);
$counter = $counterArray[0];
$lastUpdated = $counterArray[1];

// Check if a day has passed since the last update
if (date('Y-m-d', strtotime($lastUpdated)) != date('Y-m-d')) {
    $counter = 0;
    $lastUpdated = date('Y-m-d H:i:s');
}

// Increment the counter
$counter++;
file_put_contents($counterFile, $counter . '|' . $lastUpdated);

echo 'Counter: ' . $counter;