What are the best practices for handling timed actions in PHP, especially in the context of a browser game?

Handling timed actions in PHP, especially in the context of a browser game, requires a way to track the time when an action was initiated and compare it to the current time to determine if the action should be executed. One common approach is to store the timestamp of the last action in a database or session variable and then check if enough time has passed to allow the action to be performed.

// Example code for handling timed actions in PHP

// Check if enough time has passed since the last action
function canPerformAction($lastActionTimestamp, $cooldown) {
    $currentTime = time();
    return ($currentTime - $lastActionTimestamp) >= $cooldown;
}

// Usage example
$lastActionTimestamp = 1609459200; // Timestamp of the last action
$cooldown = 60; // Cooldown in seconds

if (canPerformAction($lastActionTimestamp, $cooldown)) {
    // Perform the action
    echo "Action performed!";
    // Update the last action timestamp
    $lastActionTimestamp = time();
} else {
    // Display a message indicating that the action is on cooldown
    echo "Action is on cooldown. Please wait.";
}