簡體   English   中英

格式化從SQL 2008 datetime字段返回的PHP中的日期

[英]Formatting a date in PHP returned from SQL 2008 datetime field

我在嘗試解析SQL查詢中返回的datetime字段時遇到問題。 最初我收到有關無法將其顯示為字符串的錯誤,但在下面添加了格式部分,並且至少已顯示。

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

如果這樣做,它可以工作,但是我沒有我想要的格式。 我真的需要DD MM YY HH,MM,現在我知道該怎么做了。 我需要能夠在腳本后面的日期進行計算,因此我不能僅僅將其設為字符串。 任何幫助,將不勝感激。

查看date()函數。 它允許您格式化所有想要的日期和時間。

$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
}

另一種方法是使用

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM