Twitter <created_at>值在Flash </created_at>中成为友好时间格式

时间:2011-09-02 10:25:22

标签: flash date twitter

我一直在谷歌搜索几个小时,还没有找到解决这个问题的方法。

目前,我从twitter xml文件中检索数据:http://twitter.com/statuses/user_timeline.xml?screen_name=clubbluecanopy

一切正常,我的日期格式显示:Fri Aug 12 03:25:40 +0000 2011 但我希望它能显示出来:17天前

这是我的flash as3代码:

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);



}

这是php代码:

<?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;
?>

我也使用了crossdomain.xml

如果有人可以帮助我,我们将不胜感激!谢谢! :)

1 个答案:

答案 0 :(得分:0)

Fri Aug 12 03:25:40 +0000 2011表示2011年8月12日星期五,格林尼治标准时间凌晨3点25分40秒

这是Flash的原生日期格式化字符串

您可以创建另一个功能,为您提供所需的输出:

    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";
            }
        }

您可以按如下方式使用它:

time.text= toRelativedate(myXML.status[0].created_at);
相关问题