Gnub Posted February 6, 2007 Share Posted February 6, 2007 Is there a way to change the format of a Date during a query? Example. The Date is in American Format MM/DD/YYYY When im returning the data, i want the date in Euro Format, DD/MM/YYYY Is there a way to do this during the main query(when asking for specific info) or will i be better off finding a way of doing it somewhere on my php script? Link to comment https://forums.phpfreaks.com/topic/37299-solved-changing-date-format-while-returning-data/ Share on other sites More sharing options...
trq Posted February 6, 2007 Share Posted February 6, 2007 Is there a way to change the format of a Date during a query? Use the mysql function DATE_FORMAT(). Link to comment https://forums.phpfreaks.com/topic/37299-solved-changing-date-format-while-returning-data/#findComment-178274 Share on other sites More sharing options...
Gnub Posted February 6, 2007 Author Share Posted February 6, 2007 I've had a bash with this function, but it's not liking my attempts. $sql = "Select * FROM `Teletext` WHERE DepartureDate BETWEEN '$DateFrom' AND '$DateTo' Order BY Price ASC;" Got an idea where i can add that function? Tried before @DepartureDate@ and before @ORDER BY@ . Link to comment https://forums.phpfreaks.com/topic/37299-solved-changing-date-format-while-returning-data/#findComment-178300 Share on other sites More sharing options...
trq Posted February 6, 2007 Share Posted February 6, 2007 You shouldn't use the wildcard ( * ) to select all data, but name your fields explicitly. A simple example. SELECT DATE_FORMAT(fldname,'%d/%m/%y') AS formated_date FROM tblname; Link to comment https://forums.phpfreaks.com/topic/37299-solved-changing-date-format-while-returning-data/#findComment-178333 Share on other sites More sharing options...
Gnub Posted February 13, 2007 Author Share Posted February 13, 2007 Round 3... Had yet another bash at this function, this is the query, however the date does not change when i check the results. it remains YYYY-MM-DD, when i want it at DD-MM-YYYY. Any help would be great. SELECT `Teletext`.*,DATE_FORMAT(`DepartureDate`, '%d %m %Y') AS revised_date, WHERE DepartureDate BETWEEN '$DateFrom' AND '$DateTo' Order BY Price ASC; Should i be using the `DepartureDate` when echo'ing the results? im completely new at this revised date, and not using the (*) wild card. cheers in Adv. Link to comment https://forums.phpfreaks.com/topic/37299-solved-changing-date-format-while-returning-data/#findComment-183516 Share on other sites More sharing options...
JJohnsenDK Posted February 13, 2007 Share Posted February 13, 2007 Why not convert the date in a php function? if (!function_exists('showdate2')) { function showdate2 ($date) { $year = substr($date,0,2); $month = substr($date,3,2); $day = substr($date,6,2); $rest = substr($date,9,5); return $day . "/" . $month . "-" . $year ." ".$rest; } } Link to comment https://forums.phpfreaks.com/topic/37299-solved-changing-date-format-while-returning-data/#findComment-183528 Share on other sites More sharing options...
Lodar Posted February 13, 2007 Share Posted February 13, 2007 No you need to use the new column it is now referenced as when you echo the results, so it will be revised_date you need to use. By echoing DepartureDate you are getting the original value before you applied the formatting. As thorpe has said, you really need to type in the names of the fields you require and not use *, so as an example it would be SELECT col1, col12, col3 FROM table1 WHERE col1 BETWEEN '$here' AND '$there' ORDER BY col1 desc; Link to comment https://forums.phpfreaks.com/topic/37299-solved-changing-date-format-while-returning-data/#findComment-183548 Share on other sites More sharing options...
Gnub Posted February 13, 2007 Author Share Posted February 13, 2007 cheers for the help. Managed to solve it, had to put in that 'revised_date' . Kudo's to you guys. Link to comment https://forums.phpfreaks.com/topic/37299-solved-changing-date-format-while-returning-data/#findComment-183551 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.