How can a PHP developer efficiently reset a counter at midnight for a specific user action?

To efficiently reset a counter at midnight for a specific user action in PHP, you can store the last reset timestamp in the database and compare it with the current timestamp. If the current timestamp is after midnight, reset the counter to 0. This can be achieved by checking the timestamp at the beginning of the user action and resetting the counter if needed.

// Assuming $lastResetTimestamp is fetched from the database for the specific user
$currentTime = time();
$midnight = strtotime('today midnight');

if ($currentTime >= $midnight && $lastResetTimestamp < $midnight) {
    // Reset the counter to 0
    $counter = 0;
    // Update the last reset timestamp in the database
    $lastResetTimestamp = $currentTime;
    // Save the updated timestamp in the database
}