PDA

View Full Version : displaying dates


tony09uk
04-19-2012, 09:08 PM
At last im trying to create my own programmes, but as expected im coming into a few sticking points that im struggling to overcome, please help.

this is my code to pull some posts from a database and it works fine, but it displays the date yyyy mm dd and I want to display it as dd month yy

$article = mysql_query("SELECT content, date FROM article ORDER BY date DESC");

while($row = mysql_fetch_array($article)){
echo "<p>" . $row['date'] . "<br/>" . $row['content'] . "</p>";
}

so I tried this:

$article = mysql_query("SELECT content, DATE_FORMAT(date, '%d %b %y') FROM article ORDER BY date DESC");

while($row = mysql_fetch_array($article)){
echo "<p>" . $row['date'] . "<br/>" . $row['content'] . "</p>";
}

and received the error undefined index.

what am i missing

edbr
04-20-2012, 02:14 AM
i would just select date and then format. after. is it a timestamp?

tony09uk
04-20-2012, 10:04 AM
I think so I have set it to NOW() when updates are made

edbr
04-21-2012, 01:49 AM
try

$article = mysql_query("SELECT content, DATE_FORMAT(date, '%d %b %y') as date FROM article ORDER BY date DESC");

tony09uk
04-21-2012, 10:21 AM
Right then master guru ed, that work perfectly :), many thanks! Now i have to ask why? Why was an Alias needed to make it work?

edbr
04-22-2012, 02:18 AM
to be honest ive never thought about it, its a function and you have to name it to use it as a resource