How can timestamps be effectively used to determine online user status in PHP applications?

Using timestamps in PHP applications can help determine the online status of users by updating the timestamp whenever a user interacts with the application. By comparing the current time with the user's last interaction timestamp, you can determine if the user is currently online or offline based on a specified time threshold.

// Update user's last interaction timestamp
function updateLastInteraction($userId) {
    // Update the user's last interaction timestamp in the database
}

// Check if user is online based on timestamp
function isUserOnline($lastInteractionTimestamp, $threshold = 300) {
    $currentTime = time();
    $timeDifference = $currentTime - $lastInteractionTimestamp;
    
    return ($timeDifference <= $threshold);
}

// Example of how to use the functions
$userId = 1;
updateLastInteraction($userId);

// Get user's last interaction timestamp from the database
$lastInteractionTimestamp = 1609459200; // Example timestamp

if (isUserOnline($lastInteractionTimestamp)) {
    echo "User is online";
} else {
    echo "User is offline";
}