2010-10-01 78 views
0

我遇到了問題,試圖解析我在SQL查詢中返回的日期時間字段。我最初得到的錯誤是無法將其顯示爲字符串,但添加了下面的格式部分,並且至少顯示了它。從SQL 2008格式化日期PHP datetime字段

<td>".$frontpageresultarray['SubmitDate']->format(DATE_RSS)."</td> 

如果我這樣做,它的工作原理,但我沒有得到我想要的格式。我真的需要DD MM YY HH,MM,我現在已經知道如何去做。我需要能夠在腳本後面的日期進行計算,所以我不能真正將它作爲一個字符串。任何幫助,將不勝感激。

+1

那麼,什麼格式是它的權利嗎? – 2010-10-01 14:44:07

+0

在SQL 2008數據庫中,它是日期時間格式 – Trinitrotoluene 2010-10-01 14:52:34

回答

0
$dateString = $frontpageresultarray['SubmitDate']->format(DATE_RSS); 
if($dateObject = new DateTime($dateString)){ 
// dateString was accepted 
// now do something like $dateObject->diff($otherDateObject); to calculate the difference between the $dateObject and $otherDateObject 

}else{ 
// date couldn't be converted 
// refere to [date formats][1] for possible formats to use with DateTime 
} 

另一approcha是使用

$timestamp = strtotime(string $dateString); 
$timestamp2 = strtotime(string $otherDateString); 
$difference_in_seconds = $timestamp - $timestamp2; 

$time_converted = date("d.m.Y",strtotime($dateString)); //takes the date in $time reads it converts it to unix timestamp and converts this timestamp via date back to string 
1

看看date()函數。它允許格式化日期和時間在你想要的一切。

+0

所有似乎要做的就是格式化當前日期我沒有看到使用該函數重新格式化現有字符串的方式。 – Trinitrotoluene 2010-10-01 14:57:55

+0

您可以將其轉換爲時間戳,然後根據需要使用日期功能對其進行格式化。 – lbedogni 2010-10-01 19:17:12