To make it short, my final solution is:
- store all times in the db in UTC
- display relative timings (e.g., 6m ago) in s/m/h when difference is less than 1 day
- display date (e.g., May 6) when difference is greater than 1 day, adding time only if the user saved his timezone (in particular no time for guests)
- possibly correct the time displayed via js if client-side detection works
At infrastructure level I believe that storing all times in UTC saves a lot of mental sanity. :) Modern dbms support automatic conversion from server local time to UTC (e.g., timestamp data type in mysql). Or one can configure all servers on UTC and be more flexible on data type (e.g., in mysql time/datetime would be ok).
Personally I prefer the second one. However, I've noticed that developing/debugging on my machine (which is not on UTC) may generate confusion.
Moving to application level, the most clever solution is probably to display relative timings (e.g., 6m ago) which is a good solution to avoid the problem :) However for some content, e.g. news/events, it may still be relevant to display the full time… so for the sake of completeness let's discuss it.
To the best of my knowledge from the http request there is no way to know users' timezone. So the only purely-server-side solution I see is to ask users for their timezone and the poor guests can't have timing information.
Adding client-side support, the best open source solution seems to be: https://bitbucket.org/pellepim/jstimezonedetect (there exist also jquery-plugins or other stuffs but all essentially based on this)
With such a tool one can update time information directly at client side. Many solutions I've found on the web display an html tag with time in UTC and translate it in local time via js. This way to do is not perfect when js is not working. It's probably better to use a tag that displays a relative time information and has an attribute with UTC time for correcting the content to local time (this is implemented, e.g., by Facebook).
Finally, it would be useful/nice to use client-side detection to instruct the server. In principle, the detection could be run async immediately after the js code is loaded, and the page content could be already provided by the server with correct time information (imagine the fb page is downloaded with no timeline data, the timezone detected and sent to the server, then the timeline data loaded via ajax with time properly set). However I'm not aware of any implementations and I'm wondering if the result is worth the effort.