Question

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

Was it helpful?

Solution

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 toDATE_FORMATthe 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'];

OTHER TIPS

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'];

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top