2011-10-19 59 views
1

我有一個從Facebook輸出XML格式數據的事件源。我認爲日期/時間採用時代(unix)格式。像這樣:需要幫助從XML輸出格式化Unix日期。

<start_time>1319506200</start_time><end_time>1319511600</end_time> 

這是動態信息(由facebook頁面創建的事件)。

我正在使用php file_get_contents將XML輸出放在我的html中。

如何在世界中將unix日期轉換爲用戶友好的格式?我處於全面虧損狀態。

+0

http://www.php.net/manual /en/function.date.php – alex

回答

1

只需使用string date (string $format [, int $timestamp = time() ])

從XML字符串中提取的時間戳值和參數#2傳中至今,作爲參數#1,你應該提供您的首選格式的字符串,例如「YMD T:我:■ 「

提取他時間戳很可能是這個樣子: 既然你有存儲在一個字符串的情況下進輸出數據,在這種情況下$ xmlstr

$facebooksomething = new SimpleXMLElement($xmlstr); 
date('Y-m-d T:i:s', $facebooksomething->starttime); 
+0

我對不知道的道歉,但我如何提取的價值?我瞭解日期字符串的概念,但如何提取時間戳,以通過它的PHP? – Sarah

+0

謝謝 - 我會看看我能想出什麼 - 我真的沒有得到PHP :)我幾乎沒有CSS和HTML的功能,大聲笑 – Sarah

1

最後得到這個想通了! :)嗚呼! (順便說一下從XML飼料JSON提要改變)這是結束了工作:

$start_date = date('F j, Y, g:i a', strtotime($json_output2->start_time)); 
    $end_date = date('g:i a', strtotime($json_output2->end_time)); 

這裏是我的全碼:

<?php 

$jsonurl = "https://graph.facebook.com/PAGEID/events?access_token=MYYOKEN"; 
$json = file_get_contents($jsonurl,0,null,null); 
$json_output = json_decode($json); 

foreach ($json_output->data as $data) 

{ 

$jsonurl2 = "https://graph.facebook.com/$data->id/"; 
$json2 = file_get_contents($jsonurl2,0,null,null); 
$json_output2 = json_decode($json2); 
$start_date = date('F j, Y, g:i a', strtotime($json_output2->start_time)); 
$end_date = date('g:i a', strtotime($json_output2->end_time)); 

echo "{$json_output2->name}\n"; 
echo "<br>"; 
echo "{$start_date}\n"; 
echo "&nbsp;-&nbsp;"; 
echo "{$end_date}\n"; 
echo "<br>"; 
echo "{$json_output2->description}\n"; 
echo "<br>Where: "; 
echo "{$json_output2->location}\n"; 
echo "<br><br>"; 
} 
?>