Pal Posted January 12, 2010 Share Posted January 12, 2010 Hi I wish to convert 3 individual column values - day, month and year into a string that can be read as a date and then ordered as a date? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/188276-help-converting-string-to-date/ Share on other sites More sharing options...
squiblo Posted January 13, 2010 Share Posted January 13, 2010 You mean something like this? <?php $query = mysql_query("SELECT * FROM date_table"); while ($row = mysql_fetch_assoc($query)) { $day = $row['day']; $month = $row['month']; $year = $row['year']; } echo $day ."/". $month ."/". $year ."<br />".; ?> Quote Link to comment https://forums.phpfreaks.com/topic/188276-help-converting-string-to-date/#findComment-993936 Share on other sites More sharing options...
PFMaBiSmAd Posted January 13, 2010 Share Posted January 13, 2010 Untested, but should work - ORDER BY CONCAT_WS('-',year,month,day) Quote Link to comment https://forums.phpfreaks.com/topic/188276-help-converting-string-to-date/#findComment-993940 Share on other sites More sharing options...
Pal Posted January 13, 2010 Author Share Posted January 13, 2010 thanks, once it is concatenated, will SQL read it and be able to order it as a date and not read it as text? Quote Link to comment https://forums.phpfreaks.com/topic/188276-help-converting-string-to-date/#findComment-993945 Share on other sites More sharing options...
PFMaBiSmAd Posted January 13, 2010 Share Posted January 13, 2010 It depends on exactly what format is in month and day. The code I posted assumes leading zero's so that the resulting format is yyyy-mm-dd, which is what is necessary to order dates correctly. Quote Link to comment https://forums.phpfreaks.com/topic/188276-help-converting-string-to-date/#findComment-993946 Share on other sites More sharing options...
Pal Posted January 13, 2010 Author Share Posted January 13, 2010 ah ok, unfortunately there aren't zeros so jan to sep appear as 1 - 9 and the same for days... Is there anyway of altering this in the same query? Quote Link to comment https://forums.phpfreaks.com/topic/188276-help-converting-string-to-date/#findComment-993948 Share on other sites More sharing options...
Pal Posted January 13, 2010 Author Share Posted January 13, 2010 By the way I ran it anyway despite the missing zeros, and it appears to have worked. I queried the minimum of a group of dates for each row and it has produced results, which I am assuming means it was read correctly? Quote Link to comment https://forums.phpfreaks.com/topic/188276-help-converting-string-to-date/#findComment-993950 Share on other sites More sharing options...
PFMaBiSmAd Posted January 13, 2010 Share Posted January 13, 2010 You can force it into a DATE format by applying almost any function that returns a DATE value (tested for a date like 2008-1-9) - ORDER BY DATE(CONCAT_WS('-',year,month,day)) Quote Link to comment https://forums.phpfreaks.com/topic/188276-help-converting-string-to-date/#findComment-993956 Share on other sites More sharing options...
Pal Posted January 13, 2010 Author Share Posted January 13, 2010 Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/188276-help-converting-string-to-date/#findComment-994157 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.