RidgeandGable Posted November 21, 2014 Share Posted November 21, 2014 Hi Guys In MYSQL the date format seems to be YYYY-MM-DD, when I use a dynamic table in Dreamweaver the date is also displayed this way, is there a simple way to change this to UK format DD-MM-YYYY? Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/ Share on other sites More sharing options...
mac_gyver Posted November 21, 2014 Share Posted November 21, 2014 you can either do this in the database query, using mysql's DATE_FORMAT() function, or you can do this in your php code, using php's datetime class, format() method. Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497198 Share on other sites More sharing options...
RidgeandGable Posted November 21, 2014 Author Share Posted November 21, 2014 Thanks, can you give example for doing it via mysql? I've checked the manual for this but couldn't quiet understand where to put it Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497200 Share on other sites More sharing options...
Barand Posted November 21, 2014 Share Posted November 21, 2014 SELECT DATE_FORMAT(mydatefield, '%d-%m-%Y') as formattedDate If you are sorting or comparing dates, use the original and not the formatted version Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497202 Share on other sites More sharing options...
RidgeandGable Posted November 21, 2014 Author Share Posted November 21, 2014 This is what I have in php Displaying the Field: <td><div align="center"><?php echo $row_Invoices['duebydate']; ?></div></td> Select Statement mysql_select_db($database_Harry, $Harry); $query_Invoices = sprintf("SELECT * FROM invoices WHERE username = '%s' ORDER BY invoicedate DESC", $colname_Invoices); $query_limit_Invoices = sprintf("%s LIMIT %d, %d", $query_Invoices, $startRow_Invoices, $maxRows_Invoices); $Invoices = mysql_query($query_limit_Invoices, $Harry) or die(mysql_error()); $row_Invoices = mysql_fetch_assoc($Invoices); How would I use your code in this? Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497204 Share on other sites More sharing options...
Barand Posted November 21, 2014 Share Posted November 21, 2014 Don't use SELECT *. Specify the fields you need, then it becomes simple to add in the DATE_FORMAT()... as invdate Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497206 Share on other sites More sharing options...
RidgeandGable Posted November 21, 2014 Author Share Posted November 21, 2014 (edited) So it would become Select Date_Format("[iD],[username],[Date,'%d-%m-%Y]") ? Edited November 21, 2014 by RidgeandGable Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497207 Share on other sites More sharing options...
RidgeandGable Posted November 21, 2014 Author Share Posted November 21, 2014 (edited) SELECT id,username,InvoiceNumber,DATE_FORMAT(invoicedate,%d-%m-%y) Edited November 21, 2014 by RidgeandGable Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497208 Share on other sites More sharing options...
RidgeandGable Posted November 21, 2014 Author Share Posted November 21, 2014 When I use the above code, I get an error for using sprintf Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497214 Share on other sites More sharing options...
Barand Posted November 21, 2014 Share Posted November 21, 2014 $query_Invoices = sprintf("SELECT id,username,InvoiceNumber,DATE_FORMAT(invoicedate,%d-%m-%y) as date FROM invoices WHERE username = '%s' ORDER BY invoicedate DESC", $colname_Invoices); Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497216 Share on other sites More sharing options...
RidgeandGable Posted November 21, 2014 Author Share Posted November 21, 2014 (edited) Thanks Sen Have tried what you put but still puts the same error out mysql_select_db($database_Harry, $Harry); $query_Invoices = sprintf("SELECT id,username,DATE_FORMAT(invoicedate,%d-%m-%y) as date,DATE_FORMAT(duebydate,%d-%m-%y) as date,description,invoicenumber,download,paid,DATE_FORMAT(datepaid,%d-%m-%y) as date FROM invoices WHERE username = '%s' ORDER BY invoicedate DESC", $colname_Invoices); $query_limit_Invoices = sprintf("%s LIMIT %d, %d", $query_Invoices, $startRow_Invoices, $maxRows_Invoices); $Invoices = mysql_query($query_limit_Invoices, $Harry) or die(mysql_error()); $row_Invoices = mysql_fetch_assoc($Invoices);This is row 20 - ORDER BY invoicedate DESC", $colname_Invoices); Warning: sprintf(): Too few arguments in C:\xampp\htdocs\rgroofing\Invoices.php on line 20You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 10' at line 1 Edited November 21, 2014 by RidgeandGable Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497219 Share on other sites More sharing options...
CroNiX Posted November 21, 2014 Share Posted November 21, 2014 Well, yes. sprintf() is converting the %[identifiers], which mysql is also using. So don't use sprintf() when using those mysql functions that contain %[identifier]. If you use prepared statements for your queries, this will be a non-issue plus they are way better to use anyway. Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497221 Share on other sites More sharing options...
RidgeandGable Posted November 21, 2014 Author Share Posted November 21, 2014 Ok, so is it as easy as removing all the sprintf statements? Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497222 Share on other sites More sharing options...
Barand Posted November 21, 2014 Share Posted November 21, 2014 Sorry, I copied your code in my last post. The format definition should be in quotes, as in my original post (#4) DATE_FORMAT(invoicedate,'%d-%m-%y'). And don't give them all the same alias (" as date") as you cannot then access them with $row['date'] Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497224 Share on other sites More sharing options...
RidgeandGable Posted November 21, 2014 Author Share Posted November 21, 2014 ok did that with the quotes but still got the error. Think I'll just leave it lol, I dont mind it being in reverse just gotta remember it when I'm entering the data. Cheers though Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497228 Share on other sites More sharing options...
mac_gyver Posted November 22, 2014 Share Posted November 22, 2014 if you are going to use sprintf() to build your query string, you would need to double the %% in the DATE_FORMAT() format string so that they are treated as literal % instead of a sprintf() format specifier. however, if you are using sprintf() to build your query strings, you might as well just switch to prepared queries. the %s, %d, ... sprintf() format specifiers are where you would put the prepared query place-holders, so all you would have to do is change them to ?, prepare the query, bind any input data (which are the argument(s) in the sprintf() statement), and execute the query. Quote Link to comment https://forums.phpfreaks.com/topic/292619-date-format/#findComment-1497328 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.