Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying List of Dates 1

Status
Not open for further replies.

andy98

Programmer
Jul 7, 2000
120
GB
Hi

I have a bunch of records in a table of a mySQL database and one of the fields is a DATE field in the following format (0000-00-00)

Would I am struggling to do is to format these dates on my display page. I am inserting them as NOW() - but how would I pull them out and format them (dd-mm-yyyy) within query result?

This is my query:
Code:
$query = "SELECT  * FROM customer, acc_order
WHERE customer.cust_id = acc_order.cust_id
AND acc_order_dispatched_by = ''
ORDER by acc_order.acc_order_id asc";

$result = mysql_query($query) or die (mysql_error());

And this is my output
Code:
while($row = mysql_fetch_array($result)){
echo "<td class='blue'>$row[$acc_order_date]</td>";

Does anyone know how I might format this to (dd-mm-yyyy)
 
Use the date() function:
Code:
echo "<td class='blue'>date('d-m-Y',strtotime($row[$acc_order_date]));</td>";
There's probably also a function in MySQL to format the output the way you want.

Ken
 
Hi

That simply displays...

('d-m-Y',strtotime());

on the results page!

 
It should read:
Code:
echo "<td class='blue'>".date('d-m-Y',strtotime($row[$acc_order_date]))."</td>";
 
This is my original query:

Code:
$query = "SELECT  * FROM customer, acc_order
WHERE customer.cust_id = acc_order.cust_id
AND acc_order_dispatched_by = ''
ORDER by acc_order.acc_order_id asc";

How would I go about using DATE_FORMAT for that specific query?
 
DRJ478
Your last snipet works except it now displays all rows with the same - DATE.

Shame - I thought that was the answer!
 
You want to name the date column and encapsulate it with the date_format function. I have no idea what your date column is named, so in the example I just name it myDate and alias it to the result vcar niceDate:
Code:
$query = "SELECT *, DATE_FORMAT(myDate,'%W %M %Y') AS niceDate FROM ....

Of course I gave you a silly date format, just to make sure you have a look at the MySQL manual page. ;)
 
Based upon my original query below - how would I expand that query to use the DATE_FORMAT function?

Code:
$query = "SELECT  * FROM customer, acc_order
WHERE customer.cust_id = acc_order.cust_id
AND acc_order_dispatched_by = ''
ORDER by acc_order.acc_order_id asc";

Thanks for your assistance!
 
Just as shown in my previous post.
Replace the column myDate with your actual column name and append everything after the FROM just as in your own query.
 
So that's how it's done!!! ;o)

Nice one DRJ478!

In case anyone needs an example for the future:

Code:
$query = "SELECT  *, DATE_FORMAT(acc_order_date, '%d %m %Y') AS niceDate 
FROM customer, acc_order
WHERE customer.cust_id = acc_order.cust_id
AND acc_order_dispatched_by = ''
ORDER by acc_order.acc_order_id asc";

Many thanks for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top