Maxim V. Pavlov
Maxim V. Pavlov

Reputation: 10509

Facebook-like "time since" calculation algorithm

On Facebook, each comment or other user event has a timestamp. But it is not listed as a simple date, but presented in a form of a human-friendly string. For example if right now it is 08:38 and the comment was made at 08:31, Facebook doesn't just tell you the time, but says "7 minutes ago".

Is there an open source implementation of Facebook algorithm (or similar) that takes a data of event, a current date and tells in a human-friendly form how long has it been since?

Upvotes: 0

Views: 847

Answers (3)

Abhishek Mishra
Abhishek Mishra

Reputation: 5040

You could dig into timeago jQuery plugin's source code to study.

Upvotes: 1

Wayne Rooney
Wayne Rooney

Reputation: 1607

What I think that should be the best is this :

  1. Store the timestamp when the comment is made (eg- 3:45 12-10-2012)
  2. Get the current system time
  3. Get the differnce between the two .i.e (current time -comment time ) .

Most of the languages provide the datediff method . Like in PHP you can use it like this

Upvotes: 0

Tom G
Tom G

Reputation: 3650

Pretty date http://ejohn.org/projects/javascript-pretty-date/ is a good javascript library for this

Upvotes: 4

Related Questions