I found this function on a forum somewhere and I am using it with Tweetable to show my last tweet on my website. Unfortunately, it's not grabbing time correctly. For the first hour, it says "less than a minute ago", then a couple hours go by then it changes to "less than an hour ago", then two days go by and it changes to "less than a day ago". I'm sure its something stupid in the calculation.
function relTime(time_value) {
time_value = time_value.replace(/(\+[0-9]{4}\s)/ig,"");
var parsed_date = Date.parse(time_value);
var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
var timeago = parseInt((relative_to.getTime() - parsed_date) / 1000);
if (timeago < 60) return 'less than a minute ago';
else if(timeago < 120) return 'about a minute ago';
else if(timeago < (45*60)) return (parseInt(timeago / 60)).toString() + ' minutes ago';
else if(timeago < (90*60)) return 'about an hour ago';
else if(timeago < (24*60*60)) return 'about ' + (parseInt(timeago / 3600)).toString() + ' hours ago';
else if(timeago < (48*60*60)) return '1 day ago';
else return (parseInt(timeago / 86400)).toString() + ' days ago';
}
Then its being put into the post like so:
if (defaults.time == true)
$('li#tweet-'+i).append('<p class="created-date">'+relTime(item.created_at)+'</p>');
Any help would be greatly appreciated.