RON_ron Posted August 31, 2010 Share Posted August 31, 2010 how can I change the date format to DD MM YYYY? $query = "SELECT * FROM news_home ORDER BY timestamp DESC"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/ Share on other sites More sharing options...
wildteen88 Posted August 31, 2010 Share Posted August 31, 2010 Maybe this article may help you? Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1105551 Share on other sites More sharing options...
RON_ron Posted August 31, 2010 Author Share Posted August 31, 2010 Thanks. But can someone give me an quick answer... I'm in a desperate hurry!! I'm trying to pull some data from a mysql and I'm successful in it. But I need the date format changed to DD MM YYYY. Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1105552 Share on other sites More sharing options...
jayarsee Posted August 31, 2010 Share Posted August 31, 2010 You should probably use MySQL's DATE_FORMAT() function, which will convert a date/time from whatever format it is stored in to whatever format you need it in: http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format SELECT newsname, DATE_FORMAT(newstime,'%d/%m/%Y') FROM news_home The second argument to DATE_FORMAT() can be changed around to suit your needs. Refer to the formatting table in the MySQL manual for all the options. My example would produce DD/MM/YYYY. Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1105553 Share on other sites More sharing options...
RON_ron Posted August 31, 2010 Author Share Posted August 31, 2010 Doesn't seem to work though. $query = "SELECT * FROM news_homez DATE_FORMAT(timestamp,'%d/%m/%Y') ORDER BY timestampz DESC"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) { $returnS.= $line["subjectz"].",,".$line[timestamp].",,".$line["Newsz"].",,".$line["linksz"].",,,"; } Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1105563 Share on other sites More sharing options...
jayarsee Posted August 31, 2010 Share Posted August 31, 2010 Is your timestamp field called "timestamp" or "timestampz", and is your table called "news_home" or "news_homez"? Or were you just joking around? In that example also you would need to do this: $query = "SELECT subject, DATE_FORMAT(timestamp,'%d/%m/%Y') AS timestamp, News, links FROM news_homez ORDER BY timestampz DESC"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) { $returnS.= $line["subjectz"].",,".$line["timestamp"].",,".$line["Newsz"].",,".$line["linksz"].",,,"; } Notice the AS timestamp in the SQL and the quotes around $line["timestamp"] in your while loop. Your field names were in the wrong place, they should come before the FROM. It's also generally better form to name the fields you are pulling out rather than using *. Also, double quotes invoke the PHP interpreter's interpolation scan for variables, so if you're not parsing any variables inside the string, it's more correct to do: $arrayname['arraykey'] than $arrayname["arraykey"] Because it slows the script execution down trivially, but some. It's also (arguably) more legible. Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1105565 Share on other sites More sharing options...
RON_ron Posted August 31, 2010 Author Share Posted August 31, 2010 Thanks jayarsee! But I'm getting the following error. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /php/test.php on line 9 Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1105570 Share on other sites More sharing options...
jayarsee Posted August 31, 2010 Share Posted August 31, 2010 I wanted to add that the reason the DATE_FORMAT comes before FROM is because it's essentially treated as a column name in your result set, just like the others. Since you would also not do: SELECT *,timestamp FROM table Because you would end up with 2 timestamp fields, when using a function as one of the result fields you should list out the names of the fields you want. SELECT wildcards (*) are also generally troublesome, more on my blog about that here: http://jrciiphp.blogspot.com/2010/08/in-offense-of-sql-wildcards.html Additionally, when using a MySQL function as a "field", the automatic name of the field that comes back contains the function name. Thus, to access it easily inside your while loop (like $line['timestamp']) you use the AS keyword to assign an "alias" to it. Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1105571 Share on other sites More sharing options...
jayarsee Posted August 31, 2010 Share Posted August 31, 2010 Thanks jayarsee! But I'm getting the following error. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /php/test.php on line 9 It's difficult to diagnose that because I can't see what your table looks like. Double check to make sure all the field names, and table name, matches what it says in your database. Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1105572 Share on other sites More sharing options...
fenway Posted August 31, 2010 Share Posted August 31, 2010 Maybe if you actually showed us the error? Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1105761 Share on other sites More sharing options...
RON_ron Posted September 3, 2010 Author Share Posted September 3, 2010 I'm sort of sucked with this. All that I need is to get the date which is in my mysql in this format %d/%m/%Y'. I'm not getting any data when I use code A. But works perfectly when I use code B (without that date format though). I wonder if there is any error in the syntax? code A $query = "SELECT DATE_FORMAT(timestampz, '%d/%m/%Y') FROM news_homez ORDER BY timestampz DESC"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) { $returnS.= $line["subjectz"].",,".$line["timestampz"].",,".$line["Newsz"].",,".$line["linksz"].",,,"; } echo $returnS; mysql_close($link); ?> code B $query = "SELECT * FROM news_homez ORDER BY timestampz DESC"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) { $returnS.= $line["subjectz"].",,".$line["timestampz"].",,".$line["Newsz"].",,".$line["linksz"].",,,"; } echo $returnS; mysql_close($link); ?> Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1106734 Share on other sites More sharing options...
Pikachu2000 Posted September 3, 2010 Share Posted September 3, 2010 Yes there is. $query = "SELECT DATE_FORMAT(timestampz, '%d/%m/%Y') AS timez FROM news_homez ORDER BY timestampz DESC"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) { $returnS.= $line["subjectz"].",,".$line["timez"].",,".$line["Newsz"].",,".$line["linksz"].",,,"; } echo $returnS; mysql_close($link); ?> Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1106779 Share on other sites More sharing options...
RON_ron Posted September 4, 2010 Author Share Posted September 4, 2010 . it looks all wired now... Some fields show the message 'undefined' I don't know if this is important, but in the MYSQL db I'm using the CURRENT_TIMESTAMP to add the date atuomatically in to the timestampz field. Could this have an impact to the code provided because it has got the date & time? Thanks Guys! Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1107086 Share on other sites More sharing options...
Pikachu2000 Posted September 4, 2010 Share Posted September 4, 2010 Sorry. Must have had a brainfart while writing that out. I left out the rest of the fields from the query string. Having the date and time should have no effect on DATE_FORMAT() since you specify in the function which values are displayed, and how they are displayed. The field is a DATETIME field, and the values are in there as YYYY-MM-DD HH:MM:SS, right? Can you post any errors, and whatever part of it looks weird? $query = "SELECT subjectz, DATE_FORMAT(timestampz, '%d/%m/%Y') AS timez, Newsz, linksz FROM news_homez ORDER BY timestampz DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1107089 Share on other sites More sharing options...
RON_ron Posted September 6, 2010 Author Share Posted September 6, 2010 ...The field is a DATETIME field, and the values are in there as YYYY-MM-DD HH:MM:SS, right? Can you post any errors, and whatever part of it looks weird? $query = "SELECT subjectz, DATE_FORMAT(timestampz, '%d/%m/%Y') AS timez, Newsz, linksz FROM news_homez ORDER BY timestampz DESC"; Hi Pickachu... yes it is YYYY-MM-DD HH:MM:SS. I'm getting ONLY THE DATE and NO other data. It seems only 2 lines are the idfference between the working code and the not working code. That's weird! Works alright (without the required date format from the date field). $date =date('d-m-Y'); $query = "SELECT * FROM news_homez ORDER BY timestampz DESC"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) { $returnS.= $line["subjectz"].",,".$date.",,".$line["Newsz"].",,".$line["linksz"].",,,"; } echo $returnS; mysql_close($link); ?> Pulls ONLY the date. $query = "SELECT DATE_FORMAT(timestampz, '%d/%m/%Y') AS timez FROM news_homez ORDER BY timestampz DESC"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) { $returnS.= $line["subjectz"].",,".$line["timez"].",,".$line["Newsz"].",,".$line["linksz"].",,,"; } echo $returnS; mysql_close($link); ?> Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1107735 Share on other sites More sharing options...
objnoob Posted September 6, 2010 Share Posted September 6, 2010 // without TIME in order by $query = "SELECT DATE_FORMAT(timestampz, '%d/%m/%Y') timez, Newz, linkz, subjectz FROM news_homez ORDER BY timez DESC"; // with TIME in order by $query = "SELECT DATE_FORMAT(timestampz, '%d/%m/%Y') timez, Newz, linkz, subjectz FROM news_homez ORDER BY timestampz DESC"; ORDER BY depends on whether you want to order by the formated date or by the column timestampz which also includes time. Quote Link to comment https://forums.phpfreaks.com/topic/212162-date_format/#findComment-1107745 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.