elite311 Posted January 11, 2012 Share Posted January 11, 2012 I have a column in my database storing date as DATE (shopdate is the field name) and I want to format the output from YYYY-MM-DD to Jan 12 2012 can someone help me figure out what Im doing wrong on my query? $result = $db->fetch_all_array("SELECT * FROM workshop WHERE shopdate >= CURDATE() DATE_FORMAT(shopdate,'%b %D, %Y') ORDER BY shopdate LIMIT 1"); The query works fine as long as I remove DATE_FORMAT(shopdate,'%b %D, %Y') My output look like this: <table width="100%" cellspacing="5"> <?php foreach($result as $option2) { ?> <tr> <td align="right" valign="top"><div class="shopdate">Date:</div></td> <td align="left" valign="top"><div class="shopdatedesc"><?php echo $option2["shopdate"];?></div></td> </tr> <tr> <td align="right" valign="top"><div class="shoptype">Workshop:</div></td> <td align="left" valign="top"><div class="shoptypedesc"><?php echo $option2["shoptype"];?></div></td> </tr> <tr> <td align="right" valign="top"><div class="shoptime">Time:</div></td> <td align="left" valign="top"><div class="shoptimedesc"><?php echo $option2["shoptime"];?></div></td> </tr> <?php } ?> </table> I've been doing some reading on date format but I can't figure this out. Quote Link to comment https://forums.phpfreaks.com/topic/254770-date-format/ Share on other sites More sharing options...
gin Posted January 11, 2012 Share Posted January 11, 2012 You syntax is wrong. SELECT DATE_FORMAT(shopdate,'%b %D, %Y'), shoptype, shoptime FROM workshop WHERE shopdate >= CURDATE() ORDER BY shopdate LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/254770-date-format/#findComment-1306345 Share on other sites More sharing options...
elite311 Posted January 11, 2012 Author Share Posted January 11, 2012 Interesting, so I can't just select all and than format the date? Your query worked to show 'shoptype' and 'shoptime' however the 'shopdate' doesnt show on the output, any idea why? Quote Link to comment https://forums.phpfreaks.com/topic/254770-date-format/#findComment-1306346 Share on other sites More sharing options...
gin Posted January 11, 2012 Share Posted January 11, 2012 No, I think you're thinking of one of the other languages where the formatting comes at the end. For MySQL, the formatting happens when you call the field. Ah, my mistake, here it is: SELECT DATE_FORMAT(shopdate,'%b %D, %Y') AS shopdate, shoptype, shoptime FROM workshop WHERE shopdate >= CURDATE() ORDER BY shopdate LIMIT 1 In my first response, the query would have returned a field called "DATE_FORMAT(shopdate,'%b %D, %Y')" along with "shoptype" and "shoptime". Quote Link to comment https://forums.phpfreaks.com/topic/254770-date-format/#findComment-1306348 Share on other sites More sharing options...
elite311 Posted January 11, 2012 Author Share Posted January 11, 2012 Awesome thanks! worked perfectly. Also thanks for pointing out that you cant select all than format, I' sure that will save me when making other querys Quote Link to comment https://forums.phpfreaks.com/topic/254770-date-format/#findComment-1306349 Share on other sites More sharing options...
elite311 Posted January 11, 2012 Author Share Posted January 11, 2012 I just noticed a problem, now the date order is not correct because it's ordering the dates by the names (jan, feb, mar...) instead of the actual date. i thought formatting the date would just change how it's outputted not how its sorted because the date is still stored as YYYY-MM-DD in the database Quote Link to comment https://forums.phpfreaks.com/topic/254770-date-format/#findComment-1306353 Share on other sites More sharing options...
elite311 Posted January 11, 2012 Author Share Posted January 11, 2012 Not sure if this is correct but it seems to work I changed the name after the AS to something different and then changed my output to read that new name like this: $result2 = $db->fetch_all_array("SELECT DATE_FORMAT(shopdate,'%b %D, %Y') AS newtag, shoptype, shoptime FROM workshop WHERE shopdate >= CURDATE() ORDER BY shopdate ASC LIMIT 1"); <?php echo $option2["newtag"];?> Quote Link to comment https://forums.phpfreaks.com/topic/254770-date-format/#findComment-1306354 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.