I’m creating a web-based chat/messaging system. Since the chats can be saved, each message of a chat and the time in which each message was sent are stored on the server in a mysql database. Timestamps are stored in mysql DATETIME format in UTC time and the timestamps are generated on the server.
As messages and their accompanying timestamps are retrieved from the server, I’m using the jQuery timeago plugin and formatting the timestamps to display in the form “1 minute ago”, “3 days ago”, and so on.
The problem with this is that the “timeago” time is calculated relative to the time of the user’s machine. For example, say you receive a message that was actually sent only 2 seconds ago. If your machine’s time is three minutes fast relative to the server’s time, then the time for this message displays as “3 minutes ago”.
Any ideas for strategies that will always present the accurate time to the user?
Firstly are you sure you want such a precise time synchronization for a chat system? If someone’s clock is ahead and behind, its his/her problem, not yours.
Anyway you could send across the difference to the user. The time-delta can be used to create a timestamp from the current time, and put it in the title attribute.
A simpler way would be to just send a message saying “This message was sent ‘just now'” and the title attribute is constructed accordingly.