Carla Dessi
Carla Dessi

Reputation: 9666

Twitter feed get time since posted - php

I'm using this to generate my twitter feed:

    # Access as an object
    $tweetText = $tweet->text;
    $tweetDate = $tweet->created_at;

    # Make links active
    $tweetText = preg_replace("/(http:\/\/|www.)(([^\s<]{4,68})[^\s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $tweetText);

    # Linkify user mentions
    $tweetText = preg_replace("/@(w+)/", '<a href="http://www.twitter.com/$1" target="_blank">@$1</a>', $tweetText);

    # Linkify tags
    $tweetText = preg_replace("/#(w+)/", '<a href="http://search.twitter.com/search?q=$1" target="_blank">#$1</a>', $tweetText);

    # Output
    echo $tweetText;
    echo '<br><span>Posted: ';
    echo $tweetDate;
    echo '</span>';
    echo '<br> <br>';

It currently shows the date the tweet was posted, is there a way to get it to show how long ago the tweet was posted, e.g 4 hours ago instead of a date?

$tweetDate Currently prints Tue Apr 29 09:43:17 +0000 2014

Upvotes: 0

Views: 100

Answers (1)

Rahul Kaushik
Rahul Kaushik

Reputation: 1464

Use below code :

$tweetDate  =   strtotime($tweetDate); 

$diff   =   time()-$tweetDate;

if($diff<60){
    echo $diff." sec ago";
}else if($diff<(60*60)){
    echo round($diff/60)." minute ago";
}else if($diff<(60*60*24)){
    echo round($diff/(60*60))." hours ago";
}else{
    echo round($diff/(60*60*24))." days ago";
}

Upvotes: 1

Related Questions