If you use Firefox you could write a greasemonkey script to do this. There's also a plugin that does this for you: https://addons.mozilla.org/En-us/firefox/addon/leechblock/
Its primary purpose is to block sites that are time wasters, but it also can track the time you spend on a site or block of sites.
If you use Chrome this time tracker seems to do what you want: https://chrome.google.com/webstore/detail/timestats/ejifodhjoeeenihgfpjijjmpomaphmah?hl=en