views:

459

answers:

3

Hi,

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. 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.

Cheers

+2  A: 

you need to display the "date" column that you calculate/format in the select statement, the timestamp column contains the unformatted original date value.

echo ' Posted: '.$row['date'];
KM
A: 

since in your SQL query you define the date formatting as date you access it by $row['date'].

danamlund
+3  A: 

Use backticks ( ` ) or nothing at all instead of single-quotes (') around your field in your query:

$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 DATE_FORMAT the string 'timestamp' instead of the field.

Also, since you are using as to create a field alias, you want to refer to that field using the alias when outputting:

echo ' Posted: '.$row['date'];
Andrew Moore