我想顯示一個我在我的數據庫中的時間。我設法讓它顯示我需要的正確格式的時間,但由於某種原因,它每次只顯示'4:00'。爲什麼當我試圖在PHP中使用日期函數顯示時間時,我得到'4:00'?
這裏是我的代碼:
date('g:i', strtotime($row['startTime']))
的我的例子已經在我的數據庫顯示的時間是這樣的:〇點12分30秒
爲什麼顯示「4:00」每時間?
我想顯示一個我在我的數據庫中的時間。我設法讓它顯示我需要的正確格式的時間,但由於某種原因,它每次只顯示'4:00'。爲什麼當我試圖在PHP中使用日期函數顯示時間時,我得到'4:00'?
這裏是我的代碼:
date('g:i', strtotime($row['startTime']))
的我的例子已經在我的數據庫顯示的時間是這樣的:〇點12分30秒
爲什麼顯示「4:00」每時間?
的strtotime預計datetime格式...你應該做的
date('g:i', strtotime('01 January 2009 ' . $row['startTime']))
我這樣做,但現在它顯示爲'12:00'每次 – zeckdude
檢查$ row ['startTime']有一個值,因爲它聽起來像它的空 –
什麼是底層數據庫,以及startTime列有什麼數據類型?在我最近的php代碼中進行對等,strtoime
可以在DB(MySQL)中以DATETIME表示正常工作。
startTime列是一個時間數據類型 – zeckdude
strtotime將日期時間字符串轉換爲Unix時間戳。
也許你的$ row ['startTime']沒有資格作爲日期時間字符串。
沒有一個例子here討論了日期時間字符串,其中沒有包含日期。
該鏈接還表示,如果strtotime感到困惑,它會返回隨機結果。我會添加更多的格式字符,看看還有什麼返回。
如前所述的問題是使用的strtotime()
。以下在我的機器上工作,如果它有任何用處:
$date_text = $row['startTime']; // assuming the format "00:12:30"
list($hrs,$mins,$secs) = explode(":",$date_text); // in response to the question in the comments
/* the explode() takes the string "00:12:30" and breaks into three components "00","12" and "30".
these components are named, by their order in the array formed by explode(), as $hrs, $mins and $secs.
see: http://us3.php.net/manual/en/function.explode.php
and: http://us3.php.net/manual/en/function.list.php
*/
echo "<p>" . date("g:i",mktime($hrs,$mins,$secs)) . "</p>";
其中是變量$ hrs,$ mins和$ secs來自哪裏? – zeckdude
我會更新代碼來解釋。 –
該列的數據類型是什麼?如果只是echo $ row ['startTime'],你會得到什麼? – Artelius
掛在...如果日期*存儲爲'00:12:30',並且您想顯示小時數(g)和分鐘數(i),爲什麼不簡單'echo substr($ dateText,0,5 )'哪裏'$ dateText = $ row ['startTime'];'? –
這是一個時間數據類型。當我簡單地回顯$ row ['startTime']時,什麼都沒有出現,它的奇怪的 – zeckdude