1 | <?php |
---|
2 | //-------------------------------- |
---|
3 | // Common functions and utilities |
---|
4 | //-------------------------------- |
---|
5 | |
---|
6 | // timestamp to relative period |
---|
7 | // http://tutorialzine.com/2009/09/making-our-own-twitter-timeline/ |
---|
8 | function timestamp_to_relative_time( $dt ) |
---|
9 | { |
---|
10 | $precision = 2; |
---|
11 | $times=array( 365*24*60*60 => "year", |
---|
12 | 30*24*60*60 => "month", |
---|
13 | 7*24*60*60 => "week", |
---|
14 | 24*60*60 => "day", |
---|
15 | 60*60 => "hour", |
---|
16 | 60 => "minute", |
---|
17 | 1 => "second"); |
---|
18 | |
---|
19 | $passed=time()-$dt; |
---|
20 | |
---|
21 | if($passed<5) |
---|
22 | { |
---|
23 | $output='less than 5 seconds ago'; |
---|
24 | } |
---|
25 | else |
---|
26 | { |
---|
27 | $output=array(); |
---|
28 | $exit=0; |
---|
29 | foreach($times as $period=>$name) |
---|
30 | { |
---|
31 | if($exit>=$precision || ($exit>0 && $period<60)) break; |
---|
32 | $result = floor($passed/$period); |
---|
33 | |
---|
34 | if($result>0) |
---|
35 | { |
---|
36 | $output[]=$result.' '.$name.($result==1?'':'s'); |
---|
37 | $passed-=$result*$period; |
---|
38 | $exit++; |
---|
39 | } |
---|
40 | |
---|
41 | else if($exit>0) $exit++; |
---|
42 | |
---|
43 | } |
---|
44 | $output=implode(' and ',$output).' ago'; |
---|
45 | } |
---|
46 | |
---|
47 | return $output; |
---|
48 | } |
---|
49 | |
---|
50 | // format message |
---|
51 | function format_string( $string ) |
---|
52 | { |
---|
53 | // url to link |
---|
54 | $string = preg_replace( '/((?:http|https|ftp):\/\/(?:[A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?[^\s\"\']+)/i','<a href="$1" rel="nofollow" target="blank">$1</a>', $string ) ; |
---|
55 | |
---|
56 | // some stuff for twitter, just to demonstrate ... |
---|
57 | |
---|
58 | // hashtag to link |
---|
59 | $string = preg_replace('/(^|\s)#(\w*[a-zA-Z_]+\w*)/', '\1<a href="http://twitter.com/search/\2" rel="nofollow" target="blank">#\2</a>', $string ); |
---|
60 | |
---|
61 | // @ to link |
---|
62 | $string = preg_replace('/(^|\s)@(\w*[a-zA-Z_]+\w*)/', '\1<a href="http://twitter.com/\2" rel="nofollow" target="blank">@\2</a>', $string ); |
---|
63 | |
---|
64 | return $string; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|