bschultz Posted June 9, 2009 Share Posted June 9, 2009 I have a query that pulls info from the DB. This code: echo "<a href='/recipes.php?recipe_day=$row[recipe_day]'>".DATE_FORMAT($row[recipe_day],'%M %e, %Y')." "$row[title]"</a><br />"; doesn't display the DATE_FORMAT part. As you can see, I need the raw data (column is "date" in the DB) for the link...but want the date to only be formatted for the link. Any ideas why this isn't working? I should add that it just doesn't display the date in the link...no errors. Thanks. Link to comment https://forums.phpfreaks.com/topic/161530-solved-date-format-question/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 9, 2009 Share Posted June 9, 2009 The DATE_FORMAT() function that you are using is a mysql function and is used in your SELECT query. Edit: There is a php DATE_FORMAT function but it operates on a php date object (not just a variable that contains a date), uses a different format string, and is only available in php 5.2 or higher. Link to comment https://forums.phpfreaks.com/topic/161530-solved-date-format-question/#findComment-852417 Share on other sites More sharing options...
bschultz Posted June 9, 2009 Author Share Posted June 9, 2009 since I only need the date formatted in one of the two places it's being used, would I have to assign the formatted date a variable in the select...and use $row[recipe_day] where I don't want it formatted and $newrecipeday where I do want it formatted...or use two fields in the DB? Link to comment https://forums.phpfreaks.com/topic/161530-solved-date-format-question/#findComment-852420 Share on other sites More sharing options...
PFMaBiSmAd Posted June 9, 2009 Share Posted June 9, 2009 I would use an alias in the query (what you are referring to as a variable in the select.) The value would be available as $row['newrecipeday'] (or whatever alias name you use) when the row is fetched. Link to comment https://forums.phpfreaks.com/topic/161530-solved-date-format-question/#findComment-852421 Share on other sites More sharing options...
bschultz Posted June 9, 2009 Author Share Posted June 9, 2009 Now I think I broke it even worse..I get a "no matches" echo from the query $sql = "SELECT *, DATE_FORMAT('recipe_day', '%M %e %Y') as newrecipeday, FROM bitk ORDER BY recipe_day DESC"; $rs = mysql_query($sql,$dbc); $matches = 0; while ($row = mysql_fetch_assoc($rs)) { $matches++; echo "<a href='/recipes.php?recipe_day=$row[recipe_day]'>".$row[newrecipeday]." "$row[title]"</a><br />"; } if (! $matches) { echo ("no matches"); } echo "<br />"; ?> Link to comment https://forums.phpfreaks.com/topic/161530-solved-date-format-question/#findComment-852428 Share on other sites More sharing options...
PFMaBiSmAd Posted June 9, 2009 Share Posted June 9, 2009 There should be no single-quotes around recipe_day, it is a column name, not a string - DATE_FORMAT('recipe_day', '%M %e %Y') Link to comment https://forums.phpfreaks.com/topic/161530-solved-date-format-question/#findComment-852434 Share on other sites More sharing options...
bschultz Posted June 9, 2009 Author Share Posted June 9, 2009 just for the sake of troubleshooting, I removed the ORDER BY clause...but still no matches $sql = "SELECT *, DATE_FORMAT(recipe_day, '%M %e %Y') as newrecipeday, FROM bitk"; Link to comment https://forums.phpfreaks.com/topic/161530-solved-date-format-question/#findComment-852444 Share on other sites More sharing options...
bschultz Posted June 9, 2009 Author Share Posted June 9, 2009 I had an extra comma... $sql = "SELECT *, DATE_FORMAT(recipe_day, '%M %e %Y') as newrecipeday FROM bitk ORDER BY recipe_day DESC"; THANKS! Link to comment https://forums.phpfreaks.com/topic/161530-solved-date-format-question/#findComment-852446 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.