2015-09-03 33 views
1

我想要我的輸出爲JSON,我該如何使用PHP來做這個時間戳格式?使用PHP打印JSON時間戳格式

**This 1403375851930 is not equal to 2015-09-03 11:40:46** 

{"newsfeed":[{"timeStamp": "1403375851930}]}" 

這是我電流輸出

{"newsfeed":[{"timestamp":"2015-09-03 11:40:46"}]}

PHP代碼

<?php 
$query = 'SELECT * FROM newsfeed'; 
//execute the query using mysql_query 
$result = mysql_query($query); 
//then using while loop, it will display all the records inside the table 
while ($row = mysql_fetch_array($result)) { 
$json['newsfeed'][] = $row; 
} 
?> 

輸出(時間戳是在最後

{"newsfeed":[{"0":"54","id":"54","1":"55e869fe755ea8.63620266","unique_id":"55e869fe755ea8.63620266","2":"News and Events","type":"News and Events","3":"Cosmos","title":"Cosmos","4":"http:\/\/brmhelpdesk.comlu.com\/BRMhelpdesk\/uploads\/cosmos.jpg","image":"http:\/\/brmhelpdesk.comlu.com\/BRMhelpdesk\/uploads\/cosmos.jpg","5":"asd'[sajfpsdgfg\r\nfdfhfgkljghhdsf\r\nasfdjkhjlkjghjhsdfa\r\nasfgfdgjjlkjhgsffsad","description":"asd'[sajfpsdgfg\r\nfdfhfgkljghhdsf\r\nasfdjkhjlkjghjhsdfa\r\nasfgfdgjjlkjhgsffsad","6":"http:\/\/www.brmhelpdesk.comlu.com\/BRMhelpdesk\/uploads\/lugo.png","profilePic":"http:\/\/www.brmhelpdesk.comlu.com\/BRMhelpdesk\/uploads\/lugo.png","7":"2015-09-03 11:40:46","timestamp":"2015-09-03 11:40:46"}]} 

回答

1

實現你就是最簡單的方法:

$timestamp = strtotime('2015-09-03 11:40:46'); 

你的情況:

while ($row = mysql_fetch_array($result)) { 
    $row['timestamp'] = strtotime($row['timestamp']); 
    $json['newsfeed'][] = $row; 
} 
+0

在我的循環,我將通過1宣佈他們1然後再打印作爲一個數組? –

+0

我爲你更新了我的答案。 –

+0

謝謝!有用!但我得到另一個問題打印1970年1月17日rofl好無論如何你回答了我需要再次感謝的人! –