I currently calculate the difference in time to retrieve a list of active users liek so:
$timeout = 300;
$time = strtotime("-{$timeout} sec");
$time_str = date("Y-m-d H:i:s", $time);
$SQL = "SELECT COUNT(*) AS users FROM active_users WHERE user_id={$user_id} AND last_time >= '{$time_str}'";
Can this time diff be done in a MySQL query directly? If so is there any performance impact?
Performance impact is… unknown. Depends on how efficient PHP v.s. MySQL’s date/time manipulation code is. Probably come out about the same, since they’re both probably using the same underlying libc/glibc functions anyways.