How to Implement Dynamic Timestamp in Web Page?
- by Morgan Cheng
In Facebook and twitter, we can see that there is a timestamp like "23 seconds ago" or "1 hour ago" for each event & tweet. If we leave the page for some time, the timestamp changes accordingly.
Since it is possible that user machine doesn't have same system time as server machine, how to make the dynamic timestamp accurate?
My idea is:
It is always based on server time. When request is sent to server for the web page, timestamp T1 (seconds to 1970/1/1) is rendered into inline javascript variable. The displayed timestamp ("23 seconds ago") is calculated by T1 instead of local time.
I'm not sure whether this is how Facebook/Twitter do it. Is there any better idea?