【问题标题】:Twitter <created_at> value into friendly time format at FlashTwitter <created_at> 值在 Flash 中转换为友好的时间格式
【发布时间】:2011-09-02 19:32:45
【问题描述】:

我已经在谷歌上搜索了几个小时,但还没有找到解决这个问题的方法。

目前,我从 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

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

【问题讨论】:

    标签: flash date twitter


    【解决方案1】:

    Fri Aug 12 03:25:40 +0000 2011 表示 2011 年 8 月 12 日星期五,格林威治标准时间 03 小时 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);
    

    【讨论】:

    • 您好,感谢您的帮助!但是我的文件中有一个错误:Error: 1180: Call to a possibly undefined method toRelativeDate. 我已将您的代码放在此上方:tweet_1.text = String(myXML.status[0].text); time.text= String(myXML.status[0].created_at); 我也将其放在代码上方,但错误仍然存​​在:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    相关资源
    最近更新 更多