Twitter Script Updated and Given A New Name
Back in 2009 I wrote a post on how to display twitter posts on your site. I went back to it recently and decided to tweak it further. As you see it is now updated to include ‘time since’ post. Check my sidebar for an example. I would like to thank JR over at IF Not True Then False for the handy PHP function.
The new script is now called W.O.W TweetShow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | <?php // Set timezone date_default_timezone_set("UTC"); function dateDiff($time1, $time2, $precision = 6) { if (!is_int($time1)) { $time1 = strtotime($time1); } if (!is_int($time2)) { $time2 = strtotime($time2); } if ($time1 > $time2) { $ttime = $time1; $time1 = $time2; $time2 = $ttime; } $intervals = array('year','month','day','hour','minute','second'); $diffs = array(); foreach ($intervals as $interval) { $diffs[$interval] = 0; $ttime = strtotime("+1 " . $interval, $time1); while ($time2 >= $ttime) { $time1 = $ttime; $diffs[$interval]++; $ttime = strtotime("+1 " . $interval, $time1); } } $count = 0; $times = array(); foreach ($diffs as $interval => $value) { if ($count >= $precision) { break; } if ($value > 0) { if ($value != 1) { $interval .= "s"; } $times[] = $value . " " . $interval; $count++; } } return implode(", ", $times); } $doc = new DOMDocument(); # load the RSS -- replace 'USERNAME' with your user of choice if($doc->load('http://twitter.com/statuses/user_timeline/USERNAME.rss')) { echo "<ul>\n"; # number of <li> elements to display. 20 is the maximum $max_tweets = 10; $i = 1; foreach ($doc->getElementsByTagName('item') as $node) { # fetch the title from the RSS feed. $tweet = $node->getElementsByTagName('title')->item(0)->nodeValue; #Fetch the link from the feed $link = $node->getElementsByTagName ('link') ->item(0)->nodeValue; # This gets the date and separates it into sections $pubDate = $node->getElementsByTagName('pubDate')->item(0)->nodeValue; $month = substr($pubDate, 7, 4); $date = substr($pubDate, 4, 3); $year = substr($pubDate, 11, 5); $day = substr($pubDate, 0 , 3); $title=$day.$date.$month.$year; #pre-defined lazy tags $ddmmyy=$date.$month.$year; $mmyy=$month.$year; $mmddyy=$month.$date.$year; $ddmm=$date.$month; $today = time(); $timeDiff = dateDiff($today,$pubDate,1); # the title of each tweet starts with "username: " which I want to remove $tweet = substr($tweet, stripos($tweet, ':') + 1); # OPTIONAL: turn URLs into links $tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\./-]*(\?\S+)?)?)?)@', '<a target="blank" title="$1" href="$1">$1</a>', $tweet); #OPTIONAL: turn hashtags into links $tweet = preg_replace('/#([0-9a-zA-Z_-]+)/', "<a target='blank' title='$1' href=\"http://twitter.com/search?q=%23$1\">#$1</a>", $tweet); #OPTIONAL: turn @replies into links $tweet = preg_replace("/@([0-9a-zA-Z_-]+)/", "<a target='blank' title='$1' href=\"http://twitter.com/$1\">@$1</a>", $tweet); #The following line can changed to suit your needs. #echo "<li class='tweet'>"."<strong>"."<a href='$link' target='blank' title='$title'>". $ddmm ."</a>" ."</strong>" .$tweet ."</li>\n"; # echo "<li class='tweet'>"."<em>" . "<a href='$link' target='blank' title='$title'>" . $ddmmyy."</a>" . "</em>" . $tweet . "</li>\n"; #echo "<li class='tweet'>". $tweet . "<strong>". "<a href='$link' target='blank' title='$title'>" . $ddmmyy."</a>" . "</strong>" . "</li>\n"; #echo "<li class='tweet'>" . "<strong>" . $ddmm . "</strong>" . $tweet . "</li>\n"; #echo "<li class='smallTweet'>" . $ddmm . "</li> \n"; echo "<li class='tweet'>" . $tweet . "</li>\n"; echo "<li class='smallTweet'>".'about'.' ' . $timeDiff .' '. 'ago'. "</li> \n"; if($i++ >= $max_tweets) break;} echo "</ul>\n"; }?> |
As you can see line 109 displays the time since data. Just change the CSS classes to suit your needs.

Thanks!…
Thanks for all your insight. This site has been really helpful to me….