Twitter< created_at>在Flash中的价值成为友好的时间格式 [英] Twitter <created_at> value into friendly time format at Flash

查看:151
本文介绍了Twitter< created_at>在Flash中的价值成为友好的时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经google了几个小时,还没有找到解决这个问题的办法。



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

函数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从远程文件返回字符串内容
* @author Luiz Miguel Axcar(lmaxcar@yahoo.com.br)
* /

函数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,



<如果有人可以帮助我,请欣赏!谢谢! :)

解决方案

Fri Aug 12 03:25:40 +0000 2011 表示2011年8月12日星期五,03hrs 25min 40sec GMT



这是Flash的本地日期格式化字符串



你可以创建另一个函数,它将为您提供所需的输出:

 私有函数toRelativeDate(d:Date):String {
var now:Date = new Date();
var millisecs:int = now.valueOf() - d.valueOf(); //给你num d和now之间的毫秒数
var secs:int = int(millisecs / 1000);
if(secs< 60){
return secs +seconds ago;
} else if(secs< 60 * 60){
return Math.round(secs / 60)+分钟前;
} else if(secs< 60 * 60 * 24){
return Math.round(secs /(60 * 60))+小时前
} else {
return Math.round(secs /(60 * 60 * 24))+天前;
}
}

您可以使用它如下:

  time.text = toRelativedate(myXML.status [0] .created_at); 


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

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

这篇关于Twitter&lt; created_at&gt;在Flash中的价值成为友好的时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆