PDA

View Full Version : Resolved php mysql date echo



john0611
03-02-2009, 03:04 AM
Hi, I really need some help, I have been trying to solve this problem for a while now, and i just can't get it.

I have a simple mysql data base with a few tables on it, including a Date field.

Problem is when I echo the date on my php page its displayed yyyy-mm-dd, and I need it to be displayed dd-mm-yy.

Here is the code I am using to echo the tables and feilds.

<?php

$query="select * from `news flash` ORDER BY Date ASC"; // query string stored in a variable ASC.

$rt=mysql_query($query); // query executed

echo mysql_error(); // if any error is there that will be printed to the screen
while($nt=mysql_fetch_array($rt)){
echo "<h3>$nt[Title]</h3> <p>$nt[Date]</p> <p>$nt[Content]</p>";

}

?>

hope it makes sence!

Thank you for your help and suggestions

JT

Nile
03-02-2009, 03:35 AM
Try:


<?php

function changeToDifferentDate($old){
$arr_date = explode("-", $old);
return mktime(0, 0, 0, $arr_date[1], $arr_date[2], $arr_date[0]);
}
$query="select * from `news flash` ORDER BY Date ASC"; // query string stored in a variable ASC.

$rt=mysql_query($query); // query executed

echo mysql_error(); // if any error is there that will be printed to the screen
while($nt=mysql_fetch_array($rt)){
$date = date("d-m-y", changeToDifferentDate($nt['Date']));
echo "<h3>{$nt['Title']}</h3> <p>{$date}</p> <p>{$nt['Content']}</p>";

}

?>

JasonDFR
03-02-2009, 10:05 AM
Do not use a function for this. Change your query to what is below.

SQL doesn't make you write custom functions to format dates.

$query="SELECT *, DATE_FORMAT(Date, '%d-%m-%Y') as Date FROM `news flash` ORDER BY Date ASC ";

john0611
03-02-2009, 11:02 AM
FANTASTIC!! works great!. Thank you both for all your help!:)

Nile
03-02-2009, 01:01 PM
Glad to help you! Your welcome!
It seems your topic is solved... Please set the status to resolved.. To do this:
Go to your first post ->
Edit your first post ->
Click "Go Advanced" ->
Then in the drop down next to the title, select "RESOLVED"