Question : Displaying DD/MM/YYYY from Mysql Timestamp

This is a continuation from this question:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_26356397.html?cid=239#a33289114:

I thought i had figured it out but i must be doing something wrong.

I need to show the date on the live website as DD/MM/YYYY but with using the timestamp feature on MySQL the date displays as YYYY/MM/DD and then with the time.

I use this to call the query.

$query_rs_news = "SELECT id, DATE_FORMAT(date,'%D-%m-%Y'), title, copy, link, COALESCE(image, 'spacer.jpg') AS image FROM news";

And then display the date with:

<?php echo $row_rs_news['date']; ?></p>

When i do this nothing shows?

Any ideas where i am going wrong?

I have also attached a screenshot of the DB

Attachments:

Answer : Displaying DD/MM/YYYY from Mysql Timestamp

You do this

$query_rs_news = "SELECT id, DATE_FORMAT(date,'%D-%m-%Y'), title, copy, link, COALESCE(image, 'spacer.jpg') AS image FROM news";

And then display the date with:

<?php echo $row_rs_news['date']; ?></p>

But the query has no RESULT column called "date". It *uses* date in a reformat, but the resulting field is not given a name. Try this


$query_rs_news = "SELECT id, DATE_FORMAT(date,'%D-%m-%Y') as formattedDate , title, copy, link, COALESCE(image, 'spacer.jpg') AS image FROM news";

And then display the date with:

<?php echo $row_rs_news['formattedDate']; ?></p>


I have used "formattedDate" since date is a keyword in MySQL and should really not be used or should be "back-ticked". The main change is the

DATE_FORMAT(date,'%D-%m-%Y') as formattedDate

in the SELECT.

Random Solutions  
 
programming4us programming4us