Posted By

roflman79 on 02/24/11


Tagged

php wordpress function twitter


Versions (?)

Twitter Function for Wordpress


 / Published in: PHP
 

  1. function get_twitter($username){
  2.  
  3. /* From http://www.wprecipes.com/how-to-display-your-latest-twitter-entry-on-your-wp-blog/comment-page-2#comment-114746 */
  4.  
  5. $prefix = "<p>";
  6. $suffix = "</p>";
  7.  
  8. $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
  9.  
  10. function parse_feed($feed) {
  11. $stepOne = explode("<content type=\"html\">", $feed);
  12. $stepTwo = explode("</content>", $stepOne[1]);
  13. $tweet = $stepTwo[0];
  14. $tweet = str_replace("&lt;", "<", $tweet);
  15. $tweet = str_replace("&gt;", ">", $tweet);
  16. $tweet = str_replace('&apos;', "‘", $tweet);
  17. return $tweet;
  18. }
  19.  
  20. function curr_date($feed) {
  21. $stepOne = explode("<updated>", $feed);
  22. $stepTwo = explode("</updated>", $stepOne[1]);
  23. $tweet_time = $stepTwo[0];
  24. return $tweet_time;
  25. }
  26.  
  27.  
  28.  
  29. /* If your running PHP > 5.3 you need to set this or your app will throw errors */
  30. date_default_timezone_set('Europe/London');
  31.  
  32. /* Added from Skidoosh at http://www.skidoosh.co.uk/php/create-twitter-like-date-formatted-strings-with-php/ */
  33.  
  34. function twitter_time_format ($date) {
  35.  
  36. $blocks = array (
  37. array('year', (3600 * 24 * 365)),
  38. array('month', (3600 * 24 * 30)),
  39. array('week', (3600 * 24 * 7)),
  40. array('day', (3600 * 24)),
  41. array('hour', (3600)),
  42. array('min', (60)),
  43. array('sec', (1))
  44. );
  45.  
  46. #Get the time from the function arg and the time now
  47. $argtime = strtotime($date);
  48. $nowtime = time();
  49.  
  50. #Get the time diff in seconds
  51. $diff = $nowtime - $argtime;
  52.  
  53. #Store the results of the calculations
  54. $res = array ();
  55.  
  56. #Calculate the largest unit of time
  57. for ($i = 0; $i < count($blocks); $i++) {
  58. $title = $blocks[$i][0];
  59. $calc = $blocks[$i][1];
  60. $units = floor($diff / $calc);
  61. if ($units > 0) {
  62. $res[$title] = $units;
  63. }
  64. }
  65.  
  66. if (isset($res['year']) && $res['year'] > 0) {
  67. if (isset($res['month']) && $res['month'] > 0 && $res['month'] < 12) {
  68. $format = "About %s %s %s %s ago";
  69. $year_label = $res['year'] > 1 ? 'years' : 'year';
  70. $month_label = $res['month'] > 1 ? 'months' : 'month';
  71. return sprintf($format, $res['year'], $year_label, $res['month'], $month_label);
  72. } else {
  73. $format = "About %s %s ago";
  74. $year_label = $res['year'] > 1 ? 'years' : 'year';
  75. return sprintf($format, $res['year'], $year_label);
  76. }
  77. }
  78.  
  79. if (isset($res['month']) && $res['month'] > 0) {
  80. if (isset($res['day']) && $res['day'] > 0 && $res['day'] < 31) {
  81. $format = "About %s %s %s %s ago";
  82. $month_label = $res['month'] > 1 ? 'months' : 'month';
  83. $day_label = $res['day'] > 1 ? 'days' : 'day';
  84. return sprintf($format, $res['month'], $month_label, $res['day'], $day_label);
  85. } else {
  86. $format = "About %s %s ago";
  87. $month_label = $res['month'] > 1 ? 'months' : 'month';
  88. return sprintf($format, $res['month'], $month_label);
  89. }
  90. }
  91.  
  92. if (isset($res['day']) && $res['day'] > 0) {
  93. if ($res['day'] == 1) {
  94. return sprintf("Yesterday at %s", date('h:i a', $argtime));
  95. }
  96. if ($res['day'] <= 7) {
  97. return date("\L\a\s\\t l \a\\t h:i a", $argtime);
  98. }
  99. if ($res['day'] <= 31) {
  100. return date("l \a\\t h:i a", $argtime);
  101. }
  102. }
  103.  
  104. if (isset($res['hour']) && $res['hour'] > 0) {
  105. if ($res['hour'] > 1) {
  106. return sprintf("About %s hours ago", $res['hour']);
  107. } else {
  108. return "About an hour ago";
  109. }
  110. }
  111.  
  112. if (isset($res['min']) && $res['min']) {
  113. if ($res['min'] == 1) {
  114. return "About one minut ago";
  115. } else {
  116. return sprintf("About %s minuts ago", $res['min']);
  117. }
  118. }
  119.  
  120. if (isset ($res['sec']) && $res['sec'] > 0) {
  121. if ($res['sec'] == 1) {
  122. return "One second ago";
  123. } else {
  124. return sprintf("%s seconds ago", $res['sec']);
  125. }
  126. }
  127. }
  128.  
  129. $twitterFeed = file_get_contents($feed);
  130.  
  131. return stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix).'<p class="tweet_time">'.twitter_time_format(curr_date($twitterFeed)).'</p>';
  132.  
  133. }

Report this snippet  

You need to login to post a comment.