title: Update time descriptions every minute using jquery/javascript
body: <p>I have created the following code to update the text contents of all spans like this every minute. There are numerous of these spans on the page which all need to be updated every minute:</p>
  
  <pre><code>&lt;span data-unix="1372263005" class="time_ago"&gt;4 minutes ago&lt;/span&gt;
  </code></pre>
  
  <p>The code is as follows:</p>
  
  <pre><code>window.setInterval(function(){
      $(".time_ago").each(function(i, elem) { 
          var unix = $(elem).attr("data-unix")
          var now = new Date().getTime();
          now = parseInt(now) / 1000;
          var amount = 0;
          var difference = 0;
  
          difference = parseInt(now) - parseInt(unix);
  
          if (difference &lt; 60)
          {
              $(elem).html('a few seconds ago');
          }
          else if (difference &lt; 120)
          {
              $(elem).html('a minute ago');
          }
          else if (difference &lt; 3600)
          {
              amount = Math.floor(difference / 60);
              $(elem).html(amount + ' minutes ago');
          }
          else if (difference &lt; 7200)
          {
              $(elem).html('an hour ago');
          }
          else if (difference &lt; 86400)
          {
              amount = Math.floor(difference / 3600);
              $(elem).html(amount + ' hours ago');
          }
          else if (difference &lt; 172800)
          {
              $(elem).html('a day ago');
          }
          else if (difference &lt; 2635200)
          {
              amount = Math.floor(difference / 86400);
              $(elem).html(amount + ' days ago');
          }
          else if (difference &lt; 5270400)
          {
              $(elem).html('a month ago');
          }
          else if (difference &lt; 31622400)
          {
              amount = Math.floor(difference / 2635200);
              $(elem).html(amount + ' months ago');
          }
          else if (difference &lt; 63244800)
          {
              $(elem).html('a year ago');
          }
          else
          {
              amount = Math.floor(difference / 31622400);
              $(elem).html(amount + ' years ago');
          }
      });
      return false;
  }, 60);
  </code></pre>
  
  <p>EDIT1) Ok, now I have made some changes on your advice but it's changing the span texts to 43351 years. Any ideas why it is doing that?</p>
  
  <p>EDIT2) Ok, now I have /1000 so that the php unix time matches with the js getTime(). The only problem is that now it is applying this to all the values. ie) I get 31 minutes for all the instances of .time_ago - how do I get it to apply only to one at a time so I get different times for each instance?</p>
  
  <p>EDIT3) Added correct code in case anyone finds it useful..</p>