Skip to main content Jump to list of all articles

Display your Tweets Using WOW Tweet Show

Following my previous post how to display twitter posts on your site. I decided to tweak it further by adding a time since function and I have also renamed it WOW Tweet Show. This script has been depreciated in favor of the the new Twitter API. Check out WOW-Multi-Twitter 2.0

Code

This code has since been depreciated. It has been left purely for educational purposes.

<?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('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=USERNAME'))
  {
    echo "<ul>n";
    $max_tweets = 10; #number of <li> elements to display.  20 is the maximum
    $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="https://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="https://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' . '&nbsp;' . $timeDiff . '&nbsp;' . 'ago' . "</li> n";
        
        if ($i++ >= $max_tweets)
            break;
      }
    echo "</ul>n";
  }
?>

Line 66Change USERNAME to your twitter username.
Line 124 displays the time since data.

You can style the CSS to suit your needs.


Discover more from WorldOWeb

Subscribe to get the latest posts sent to your email.

1 reply on “Display your Tweets Using WOW Tweet Show”

Comments are closed.