kwdelre Posted January 25, 2011 Share Posted January 25, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/225660-trouble-with-date_format/ Share on other sites More sharing options...
Pikachu2000 Posted January 25, 2011 Share Posted January 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225660-trouble-with-date_format/#findComment-1165123 Share on other sites More sharing options...
kwdelre Posted January 25, 2011 Author Share Posted January 25, 2011 Thank you sir! That worked! Quote Link to comment https://forums.phpfreaks.com/topic/225660-trouble-with-date_format/#findComment-1165126 Share on other sites More sharing options...
kwdelre Posted January 25, 2011 Author Share Posted January 25, 2011 I wanted to ask you about this syntax. Obviously I'm a beginner 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. Quote Link to comment https://forums.phpfreaks.com/topic/225660-trouble-with-date_format/#findComment-1165141 Share on other sites More sharing options...
Pikachu2000 Posted January 25, 2011 Share Posted January 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225660-trouble-with-date_format/#findComment-1165155 Share on other sites More sharing options...
kwdelre Posted January 25, 2011 Author Share Posted January 25, 2011 Thanks a lot Pikachu. Go to know for the future. Quote Link to comment https://forums.phpfreaks.com/topic/225660-trouble-with-date_format/#findComment-1165214 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.