How can a PHP script be written to count the minutes a user spends in a chat and display it in their profile?

To count the minutes a user spends in a chat and display it in their profile, you can store the timestamp when the user enters the chat and then calculate the difference in minutes when they leave. This information can then be saved in the user's profile for display.

// Assuming the user enters the chat
$_SESSION['chat_enter_time'] = time(); // Store the timestamp when user enters the chat

// Assuming the user leaves the chat
$chat_enter_time = $_SESSION['chat_enter_time'];
$chat_leave_time = time();
$minutes_spent_in_chat = round(($chat_leave_time - $chat_enter_time) / 60); // Calculate the minutes spent in chat

// Save the minutes spent in chat to the user's profile
$user_id = 1; // Replace with the actual user ID
// Update the user's profile with the minutes spent in chat
// For example, if using a database:
// $query = "UPDATE users SET minutes_spent_in_chat = $minutes_spent_in_chat WHERE id = $user_id";
// mysqli_query($conn, $query);