2011-09-02 40 views
0

我一直在Google上搜索幾個小時,並且還沒有找到解決這個問題的方法。推特<created_at>價值轉換爲友好的時間格式在Flash

目前,我retrive從Twitter的XML文件中的數據:http://twitter.com/statuses/user_timeline.xml?screen_name=clubbluecanopy

一切正常,我的日期格式顯示了這個:週五08月12日3時25分40秒+0000 2011 但我想讓它顯示這個:17天前

這裏是我的閃存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 ([email protected]) 
*/ 

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以及

如果有人能幫助我,我將不勝感激!謝謝! :)

回答

0

Fri Aug 12 03:25:40 +0000 2011意味着週五,2011年8月12日,03hrs 25分鐘40秒GMT

這是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); 
+0

您好,感謝˚F或你的幫助!但在我的文件中有一個錯誤:'錯誤:1180:調用一個可能未定義的方法toRelativeDate.'我把你的代碼放在這個上面:'tweet_1.text = String(myXML.status [0] .text); time.text = String(myXML.status [0] .created_at); '我也把它放在上面,在代碼下面,但是錯誤仍然存​​在:( – Misty

+0

omg它現在有效!我有多麼愚蠢!非常感謝兄弟!XD – Misty