Output latest 10 tweets


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?
  2. function ago($timestamp){
  3. $difference = time() - $timestamp;
  4.  
  5. if($difference < 60)
  6. return $difference." seconds ago";
  7. else{
  8. $difference = round($difference / 60);
  9. if($difference < 60)
  10. return $difference." minutes ago";
  11. else{
  12. $difference = round($difference / 60);
  13. if($difference < 24)
  14. return $difference." hours ago";
  15. else{
  16. $difference = round($difference / 24);
  17. if($difference < 7)
  18. return $difference." days ago";
  19. else{
  20. $difference = round($difference / 7);
  21. return $difference." weeks ago";
  22. }
  23. }
  24. }
  25. }
  26. }
  27.  
  28. $username = "username here";
  29. $format='json'; // set format
  30. $tweets=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); // get tweets and decode them into a variable
  31. $tweet_count = 10;
  32. ?>
  33. <h5>LATEST TWEET<? if($tweet_count): ?>S<? endif ?></h5>
  34. <?
  35. foreach(array_slice($tweets, 0, $tweet_count) as $tweet):
  36. $time = ago(strtotime($tweet->created_at));
  37. $content = $tweet->text; // show latest tweet
  38. //<a href="http://twitter.com/#!/status/username/39762213950459904">
  39. ?>
  40. <small><?= $time ?></a></small>
  41. <p><?= $content ?></p>
  42. <? endforeach ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.