Twitter <created_at> value into friendly time format at Flash
I have been googling for hours and have not yet found a solution to this problem.
currently, I retrive the data from the twitter xml file: http://twitter.com/statuses/user_timeline.xml?screen_name=clubbluecanopy
everything works fine, my date format shows this: Fri Aug 12 03:25:40 +0000 2011 But I want it to show this: 17 days ago
here's my flash as3 code:
var myXMLLoader:URLLoader = new URLLoader();
//myXMLLoader.load(new URLRequest("http://twitter.com/statuses/user_timeline.xml?screen_name=arunshourie"));
myXMLLoader.load(new URLRequest("twitter.php"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void{
var myXML:XML = new XML(e.target.data);
trace(myXML.status[0].text);
tweet_1.text = String(myXML.status[0].text);
time.text= String(myXML.status[0].created_at);
}
Here's the php code:
<?php
/*
* @return string
* @param string $url
* @desc Return string content from a remote file
* @author Luiz Miguel Axcar (lmaxcar@yahoo.com.br)
*/
function get_content($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
#usage:
$content = get_content ("http://twitter.com/statuses/user_timeline.xml?screen_name=clubbluecanopy");
echo $content;
?>开发者_StackOverflow;
I have used crossdomain.xml as well
would appreciate if someone can help me! thanks! :)
Fri Aug 12 03:25:40 +0000 2011
means Friday, 12th August 2011, 03hrs 25min 40sec GMT
This is Flash's native date formatted string
You can create another function which will give you the required output:
private function toRelativeDate(d:Date):String {
var now:Date=new Date();
var millisecs:int=now.valueOf()-d.valueOf(); //gives you the num. of milliseconds between d and now
var secs:int=int(millisecs / 1000);
if(secs < 60) {
return secs + " seconds ago";
}else if(secs < 60*60) {
return Math.round(secs / 60) + " minutes ago";
} else if(secs < 60*60*24) {
return Math.round(secs / (60*60)) + " hours ago";
} else {
return Math.round(secs / (60*60*24)) + " days ago";
}
}
You could use it as follows:
time.text= toRelativedate(myXML.status[0].created_at);
精彩评论