How to calculate relative time like facebook

I’ve found this cool snippet witch returns the relative time from a given date.

Just like facebook does posted 20 minutes ago.

You can translate it from English to any language. Just make sure you edit the $periods array.

Tags:

4 Responses to “How to calculate relative time like facebook”

  1. Mazen October 26, 2011 at 5:07 pm #

    Genius… Will try to apply it on an iPhone app… thanks

  2. Sergio February 19, 2013 at 9:16 am #

    HI, GOOD CODE this is using foreach

    • Rick February 19, 2013 at 9:38 am #

      Thanks for the tip !

  3. kevin September 26, 2013 at 5:53 pm #

    In order to add an “s” to the end of the current period, it looks as though you may want to move the

    logic to just AFTER the

    loop. Otherwise it seems to only add plural endings to all periods prior to the one that really matters.

Leave a Reply

Add <code> Some Code </code> by using this tags.