[英]Getting mySQL date_format to display in PHP
這對許多人來說聽起來像是一個非常簡單的問題,但我似乎無法獲得基本的date_format來處理我的mySQL語句然后使用php顯示。 這是我目前的代碼:
$result = mysql_query("SELECT *, DATE_FORMAT('timestamp', '%W %D %M %Y') as date FROM articleDB WHERE userID='".$_SESSION["**"]."' ORDER BY timestamp DESC LIMIT 8");
然后嘗試使用以下方式顯示它:
echo ' Posted: '.$row['timestamp'].'';
我想要的是將日期從PHP myAdmin時間戳格式化為我想要的格式。
干杯
在查詢中的字段周圍使用反引號(`` ) or nothing at all instead of single-quotes (
'`):
$result = mysql_query("SELECT *, DATE_FORMAT(`timestamp`, '%W %D %M %Y') as date FROM articleDB WHERE userID='".$_SESSION["**"]."' ORDER BY timestamp DESC LIMIT 8");
反引號(`` ) creates a reference to a table member, single-quotes creates a string (
' ). You were basically trying to
). You were basically trying to
DATE_FORMAT the string
'timestamp'而不是字段。
此外,由於使用的是as
創建一個字段別名,你想用的時候輸出別名來引用該字段:
echo ' Posted: '.$row['date'];
您需要在select語句中顯示您計算/格式化的“日期”列,時間戳列包含未格式化的原始日期值。
echo ' Posted: '.$row['date'];
因為在您的SQL查詢中,您將日期格式定義as date
您通過$row['date']
訪問它$row['date']
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.