Jump to content

Trouble with DATE_FORMAT


kwdelre

Recommended Posts

Hey guys. I've tried everything that I can find from searching but still cannot manage to get this to work.

 

I am simply running a SELECT query that is returning a 'date' field. I want to format this date as it is echoed. From all of the different examples I have seen I haven't seen the code in actual working code. Using the code below I get the date printed still but it is still in mysql's format. So here is the last attempt I made.

 

$QuizQuery = mysql_query("SELECT DATE_FORMAT('CURDATE()','%W, %M, %Y')
					 									Quiz_id, Score, Quiz_Name, Date_Taken, Pass
					 									FROM ".$QuizResultsTable." 
														WHERE User_id = '".$_SESSION['User_id']."' 
														AND Quiz_Name = 'P".$ii."' ");

 

I also tried using the DATE_FORMAT in the originating INSERT query with no result. Thanks for any help!

Link to comment
Share on other sites

You aren't formatting the date coming from the database with that query, you're formatting a string with the value of 'CURDATE()' (which should be returning NULL).

 

$QuizQuery = mysql_query("SELECT DATE_FORMAT(`Date_Taken`, '%W, %M, %Y') as `formatted_date`,
                                              Quiz_id, Score, Quiz_Name, Pass
                                              FROM ".$QuizResultsTable."
                                             WHERE User_id = '".$_SESSION['User_id']."'
                                             AND Quiz_Name = 'P".$ii."' ");

 

After you run the query and fetch the (associative) array, the formatted date will be in the ['formatted_date'] index of the arrray.

Link to comment
Share on other sites

I wanted to ask you about this syntax. Obviously I'm a beginner  :D

 

In your code response you used acentos around the original field name. In all examples from mysql.dev they use apostrophes. When I went to change your acentos to apostrophes (only to keep consistency) the date was NULL.

 

Does it have to be acentos? Just trying to learn it correctly, thanks for your explanation.

Link to comment
Share on other sites

When enclosing a field or table name, it is done with the `backticks` (MySQL-specific),  whereas string values are enclosed in 'single quotes'. It normally isn't necessary to enclose field and table names unless it happens to be a MySQL reserved word.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.