PHP: Twitter widget

I decided to create a PHP widget for Twitter because I was honestly tired of using JavaScript widgets which have the major drawback of causing accessibility problems in those browsers that don't support JavaScript at all. Two things came to my attention: how to replace text URLs with HTML links and how to properly calculate the time elapsed from your latest tweet. I addressed the first problem by using regular expressions, while the second was quickly solved thanks to a careful reading of some good tutorials on the subject. So I created the TwitterWidget class, which is as follows:

/** @name TwitterWidget
    @author Gabriele Romanato
    This class accepts two parameters passed to its constructor:
    1. $username = Your Twitter username
    2. $limit = How many tweets you want to display */

class TwitterWidget

  protected $_username;
  protected $_limit;
  public function __construct($username, $limit)
    $this->_username = $username;
    $this->_limit = $limit;
  /** @param String $text The text of each tweet
      @returns String $replaced A string with all URLs turned into <a> elements */
  protected function _replaceTextURLs($text)
    $re = '/(http:\/\/[^\s&]+)/';
 $replaced = preg_replace($re, '<a href="$1">$1</a>', $text);
 return $replaced;
  /** @param String $time A Unix timestamp
      @returns String The elapsed time for each tweet */
  protected function _timeAgo($time)
   $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
   $lengths = array("60","60","24","7","4.35","12","10");

   $now = time();

       $difference     = $now - $time;
       $tense         = "ago";

   for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
       $difference /= $lengths[$j];

   $difference = round($difference);

   if($difference != 1) {
       $periods[$j].= "s";

   return "$difference $periods[$j] ago ";
  /** @param None
      @returns The following markup string:
      <div class="twitter-widget">
        <div class="tweet">
          <p>Tweet's content</p>
          <div class="tweet-time-ago">Elapsed time for each tweet</div>
          <!--more tweets-->
      </div> */
  public function outputWidget()
    $tweets = simplexml_load_file(''. $this->_username . '.rss');
 $output = '<div class="twitter-widget">' . "\n";
 $i = 0;
 do {
   $tweet = $tweets->channel->item[$i];
   $text = str_replace($this->_username . ':', '', $tweet->title);
   $text = $this->_replaceTextURLs($text);
   $time = strtotime($tweet->pubDate);
   $output .= '<div class="tweet">' . 
   "\n". '<p>' . $text . '</p>' . "\n" .
   '<div class="tweet-time-ago">' . 
   $this->_timeAgo($time) . '</div>' . "\n" . 
   '</div>' . "\n";
 } while($i < $this->_limit);
 $output .= '</div>' . "\n";
 return $output;


A practical example:

$twitterWidget = new TwitterWidget('gabromanato', 5);

<!--markup for the head element-->

echo $twitterWidget->outputWidget();


This class accepts two parameters passed to its constructor:

  1. $username: your Twitter username
  2. $limit: the number of tweets you want to display

You can use this class in many contexts, including Wordpress and other CMS systems. You can see the demo below.


Live demo

Leave a Reply

Note: Only a member of this blog may post a comment.