简体   繁体   English

让mySQL date_format在PHP中显示

[英]Getting mySQL date_format to display in PHP

This could sound like a very simple question to many of you, but I seem to be having trouble getting a basic date_format to work with my mySQL statement and then to be displayed using php. 这对许多人来说听起来像是一个非常简单的问题,但我似乎无法获得基本的date_format来处理我的mySQL语句然后使用php显示。 Here is the code that I currently have: 这是我目前的代码:

$result = mysql_query("SELECT *, DATE_FORMAT('timestamp', '%W %D %M %Y') as date FROM articleDB WHERE userID='".$_SESSION["**"]."' ORDER BY timestamp DESC LIMIT 8");

Then trying to display it using: 然后尝试使用以下方式显示它:

echo ' Posted: '.$row['timestamp'].''; 

All I want is to format the date from a PHP myAdmin timestamp to the format I want. 我想要的是将日期从PHP myAdmin时间戳格式化为我想要的格式。

Cheers 干杯

Use backticks ( `` ) or nothing at all instead of single-quotes ( '`) around your field in your query: 在查询中的字段周围使用反引号(`` ) 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");

Backticks ( `` ) creates a reference to a table member, single-quotes creates a string ( ' ). You were basically trying to 反引号(`` ) 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'` instead of the field. ). You were basically trying to DATE_FORMAT the string 'timestamp'而不是字段。

Also, since you are using as to create a field alias, you want to refer to that field using the alias when outputting: 此外,由于使用的是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM