What is the purpose of using a timestamp in PHP scripts for tracking user activity?
When tracking user activity in PHP scripts, using a timestamp allows you to record the exact date and time when a certain action occurred. This can be useful for monitoring user behavior, analyzing trends, and troubleshooting issues. By including timestamps in your tracking data, you can easily sort and filter the information based on time intervals.
// Example of using a timestamp to track user activity
$user_id = 123;
$action = 'logged in';
$timestamp = date('Y-m-d H:i:s');
// Save the user activity with timestamp to a log file
$log_message = "$timestamp - User $user_id $action\n";
file_put_contents('user_activity.log', $log_message, FILE_APPEND);