Determining when a user connects to a website for the first time should be pretty easy with the proper use of Sessions.
The tricky part would be determining when the client leaves the website. The best you could do would be to monitor the user's activity on the website, and then execute a script when the user has been inactive for several minutes. I imagine such a solution would use cron tasks to identify users which are recently inactive, and then execute a PHP script.
I hope that helps. Good luck!