Getting mySQL date_format to display in PHP

This may seem like a very simple question for many of you, but I seem to be having problems getting the base date_format parameter to work with my mySQL statement and then to display using php. Here is the code I 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 try to display it using:

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

All I want is to format the date from the PHP myAdmin timestamp in the format I want.

Greetings

+3
source share
3 answers

Use backlinks (`` ) or nothing at all instead of single-quotes ('') around your field in the request:

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

, as , :

echo ' Posted: '.$row['date'];
+7

"date", / select, timestamp .

echo ' Posted: '.$row['date'];
+2

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

0
source

Source: https://habr.com/ru/post/1714624/


All Articles