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!

Date formatting from the mysql database 1

Status
Not open for further replies.

kupe

Technical User
Sep 23, 2002
376
PHP brings up news items from a MySQL database. There's a headline, byline, dateline, and the story.

It works well using the following script, which (being new to php) I have adapted. BUT it won't let me format the date from a date field in the database.

$result = @mysql_query('SELECT newsdate FROM jokes WHERE newsid="1"'); if (!$result) { exit('Error performing query: ' . mysql_error() ); }// Display the date in a paragraph while ($row = mysql_fetch_array($result)) {
echo $row['newsdate']; }
It works like this, but won't take a DATE_FORMAT(newsdate, '%b %e'). I have spent an age trying every variation I can think of, without any success. H e l p, please, Experts!
 
Those code actually is laid out like this -

$result = @mysql_query('SELECT jokedate FROM jokes WHERE jokeid="1"');

if (!$result) {
exit('Error performing query: ' . mysql_error() );
}// Display the date in a paragraph
while ($row = mysql_fetch_array($result)) {
echo $row['jokedate'];
}
 
How about:
Code:
$result = mysql_query('SELECT DATE_FORMAT(jokedate, '%b %e') as jokedateformatted FROM jokes WHERE jokeid="1"');    
if (!$result)
{
   exit('Error performing query: ' . mysql_error()  );
}
//   Display the date  in a paragraph                
while ($row = mysql_fetch_array($result)) 
{
   echo $row['jokedateformatted'];
}
 
Ooops, there's another problem with that. You are using single quotes to delimit SQL field, but then you use single quotes in DATE_FORMAT as well. That will break your code. Sheesh, I focus too much on one thing:
Code:
$result = mysql_query("SELECT DATE_FORMAT(jokedate, '%b %e') AS jokedateformatted FROM jokes WHERE jokeid='1'");
if (!$result)
{
   exit('Error performing query: ' . mysql_error()  );
}
//   Display the date  in a paragraph                
while ($row = mysql_fetch_array($result))
{
   echo $row['jokedateformatted'];
}
 
That's great, Vagabond, and thank you very much for both points. Looking forward to trying that tonight. Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top