Format Twitter time created_at

Twitter statuses comes in the usual retrievable ugly format

1
Sun Jul 03 16:11:11 +0000 2011

From this, its hard to tell whether it was 5 minutes ago or 5 hours ago.
What we need to do is get the time right now and find out how long ago it is from this time.

1
2
3
4
function from_apachedate($date){
    list($D,$M,$d,$h,$m,$s,$y) = sscanf($date, "%s %s %2d %2d:%2d:%2d +0000 %4d");
    return strtotime("$d $M $y $h:$m:$s");
}

Before I get credit for this, I found this from another site (NOTE : it was broken, and I nicely notified the owner of my fix).

What the function above does is return the twitter time in UNIX format. Take what is returned and run it through the function below :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function elapsedtime($time){
    $gap = time()- $time;
                           
   if ($gap < 60)
        return $gap.' seconds ago';
    $gap = round($gap / 60);
    if ($gap < 60)
        return $gap.' minute'.($gap > 1 ? "s" : "").' ago';
    $gap = round($gap / 60);
    if ($gap < 24)
        return 'about '.$gap.' hour'.($gap > 1 ? "s" : "").' ago';
    $gap = round($gap / 24);
    if ($gap > 24)
        return round($gap).' days ago';
        return date('h:i A F d, Y', $time);
}

This function was a whole 16 characters smaller than the smallest function I could find… I know it’s not much, but it’s for those fellow kilobyte crunchers :) .

Get Twitter status on your site/blog

Updated the site and wanted to have a nice little Twitter box.  So I Googled “twitter status on site.” Nearly every result had the same twitter status function, which only retrieved status and removed (fixed?) hyperlinks.

I also wanted the time since the post and any hash tags to have a link to Twitter.  Also to remove the deprecated ereg_replace with preg_replace.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function get_status($twitter_id, $hyperlinks = true, $hash = true) {
    $twitter = Array();
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=1");
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $src = curl_exec($c);
    curl_close($c);
    preg_match('/<created\_at>(.*)<\/created\_at>/', $src, $time);
    preg_match('/<text>(.*)<\/text>/', $src, $m);
   
    $status = htmlentities($m[1]);
    ($hyperlinks)? $status = preg_replace('/(http\:\/\/[\w\.\-\/]*\w)(?=.*)/','<a target="_blank" href="$1">$1</a>',$status) : $status = $status;
   
    $twitter['time'] = $time[0];
    ($hash)? $twitter['status'] = preg_replace('/\#(\w*)/','<a target="_blank" href="http://twitter.com/#!/search/$1">#$1</a>',$status) : $twitter['status'] = $status;
    return($twitter);
}

So examples!

1
2
3
4
5
6
7
8
9
//ALL
$twitter = get_status("HostGrenade");

print_r($twitter);

//HASH TAGS, BUT NOT HYPERLINKS
$twitter = get_status("HostGrenade",false,true);

print_r($twitter);




UPDATE : JANUARY 7, 2012

Working on a WordPress theme, I decided that this script needed to be updated to allow more than 1 status to come back. The function has changed as well as the call made to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function get_status($twitter_id, $count = 3, $hyperlinks = true, $hash = true) {
        $twitter = Array();
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=$count");
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        $src = curl_exec($c);
        curl_close($c);
        $xml = new SimpleXMLElement($src);
        $i=1;

        foreach($xml->status as $key=>$var){
            ($hyperlinks)? $var->text = preg_replace('/(http\:\/\/[\w\.\-\/]*\w)(?=.*)/','<a target="_blank" href="$1">$1</a>',$var->text) : $var->text = $var->text;
            ($hash)? $var->text = preg_replace('/\#(\w*)/','<a target="_blank" href="http://twitter.com/#!/search/$1">#$1</a>',$var->text) : $var->text = $var->text;
            $twitter[] = array("status" => $var->text,"time"=>elapsedtime(from_apachedate($var->created_at))); 
            if($i > $count){
                break;
            }
        }
        return($twitter);
    }