Lost your password?

Easy Twitter/Website Integration!

Below is an extremely simple piece of code to bring in up to 5 twitter “tweets”, each styled by a div class called ‘tweet’.

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
<?php  
 
// Your twitter username.  
$username = "killainstnct";  
 
$no_of_tweets = 5;
 
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $no_of_tweets;
 
$xml = simplexml_load_file($feed);
foreach($xml->children() as $child) {
	foreach ($child as $value) {
		if($value->getName() == "link") {
			$link = $value['href'];
 
		}
		if($value->getName() == "content") {
			$content = $value . "<br />";
			echo '<div class="tweet">' . $content;
		}
		if($value->getName() == "updated") {
			$updated = $value;
			$updatedunix = strtotime($updated);
			$updated = date(" g:i a F j, Y", $updatedunix);
			$time = time();
			$time_period = $time - $updatedunix;	
 
			if($time_period < 60) {
				//Seconds
				$seconds = $time_period;
				if($seconds == 1) {
				$updated = 'tweeted ' .$seconds. ' second ago';
				} else {
				$updated = 'tweeted ' .$seconds. ' seconds ago';
				}
			} else if ($time_period < 3600) {
				//Minutes
				$minutes = floor(($time_period / 60));
				$seconds = floor($time_period - $minutes * 60);
				if($minutes == 1) {
					if($seconds == 1) {
						$updated = 'tweeted ' .$minutes. ' minute, ' . $seconds . ' second ago';
					} else {
						$updated = 'tweeted ' .$minutes. ' minute, ' . $seconds . ' seconds ago';
					}
				} else {
					if($seconds == 1) {
						$updated = 'tweeted ' .$minutes. ' minutes, ' . $seconds . ' second ago';
					} else {
						$updated = 'tweeted ' .$minutes. ' minutes, ' . $seconds . ' seconds ago';
					}
				}
			} else if ($time_period < 86400)  {
				//Hours
				$minutes = ($time_period / 60);
				//echo $minutes;
				$hours = floor(($minutes / 60));
				//echo $hours;
				$minutes = floor($minutes - ($hours * 60));
				if($hours == 1) {
					if($minutes == 1) {
						$updated = 'tweeted ' .$hours. ' hour, ' . $minutes . ' minute ago';
					} else {
						$updated = 'tweeted ' .$hours. ' hour, ' . $minutes . ' minutes ago';
					}
				} else {
					if($minutes == 1) {
						$updated = 'tweeted ' .$hours. ' hours, ' . $minutes . ' minute ago';
					} else {
						$updated = 'tweeted ' .$hours. ' hours, ' . $minutes . ' minutes ago';
					}
				}	
			} else if ($time_period > 86400) {
				//Days	
				$minutes = ($time_period / 60);
				$hours = floor(($minutes / 60));
				$days = floor(($hours / 24));
				$hours = floor($hours - ($days * 24));
				if($days == 1) {
					if($hours == 1) {
						$updated = 'tweeted ' .$days. ' day, '.$hours.' hour ago';
					} else {
						$updated = 'tweeted ' .$days. ' day, '.$hours.' hours ago';
					}
 
				} else {
					if($hours == 1) {
						$updated = 'tweeted ' .$days. ' days, '.$hours.' hour ago';
					} else {
						$updated = 'tweeted ' .$days. ' days, '.$hours.' hours ago';
					}
				}
			}
 
			echo '<a href="'.$link.'">' . $updated . '</a><br /></div>';
		}			
	}
}	
 
?>

Feel free to copy the code, just edit the username, and the number of tweets. Also, You MUST have simpleXML installed to use this.

Will post an actual post later, and sort out images for this, the last post and the next one!

Chur, Killa

EDIT: Added {Minutes, Seconds}, {Hours, Minutes} & {Days, Hours} formatting, this is easy to remove but its extremely accurate :)

  • Mauro
    November 13, 2009

    Hey, thanks for the code, really works for me.

  • bilal
    December 31, 2009

    nice one but its not showing more then 1 tweet :( and also not refreshing latest tweet rapidly

  • bilal
    December 31, 2009

    if my latest tweet is more then 1 day older then it will not display

  • Nix
    January 20, 2010

    This is great… How do you deactivate the entire time formatting?

  • killainstnct
    February 2, 2010

    you’d need to hack into the code for that, I’m probably going to work on a way of loading everything into an array in the future, as this is by far the easiest way for me to put Ajax with it as well.

Leave a comment