bballer Posted November 25, 2010 Share Posted November 25, 2010 New user of PHP here! I'm using it to display entries from my MYSQL database. Image below. This is the code snippet that displays information from the 'name', 'author' and 'date' columns of my database. I learned this technique from w3schools. $result = mysql_query("SELECT * FROM levels ORDER BY date DESC"); while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['author'] . "</td>"; echo "<td align='right'>" . $row['date'] . "</td>"; echo "</tr>"; } I would like to use the date() function to format the date differently. But the date() function requires that I convert my time into a timestamp. I know you have to use the mktime() function for this, but I don't know how. Can someone show me how to pass the contents from $row[date] into the mktime() function? Link to comment https://forums.phpfreaks.com/topic/219775-help-with-date-and-mktime-functions/ Share on other sites More sharing options...
Pikachu2000 Posted November 25, 2010 Share Posted November 25, 2010 How is the date currently stored in the database? What is the data type of the field it's stored in? Link to comment https://forums.phpfreaks.com/topic/219775-help-with-date-and-mktime-functions/#findComment-1139339 Share on other sites More sharing options...
bballer Posted November 25, 2010 Author Share Posted November 25, 2010 It's currently stored as: 2010-11-24 And the data type is DATE. Link to comment https://forums.phpfreaks.com/topic/219775-help-with-date-and-mktime-functions/#findComment-1139343 Share on other sites More sharing options...
Pikachu2000 Posted November 25, 2010 Share Posted November 25, 2010 Much simpler to format it in the query using MySQL's native DATE_FORMAT() function, as long as the data is stored properly (which it is). $result = mysql_query("SELECT `name`, `author`, DATE_FORMAT(`date`, '%b-%e-%Y') as `form_date` FROM `levels` ORDER BY `date` DESC"); while( $row = mysql_fetch_array($result) ) { { echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['author'] . "</td>"; echo "<td align='right'>" . $row['form_date'] . "</td>"; echo "</tr>"; } Link to comment https://forums.phpfreaks.com/topic/219775-help-with-date-and-mktime-functions/#findComment-1139349 Share on other sites More sharing options...
bballer Posted November 25, 2010 Author Share Posted November 25, 2010 That's way easier. Thanks! Link to comment https://forums.phpfreaks.com/topic/219775-help-with-date-and-mktime-functions/#findComment-1139352 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.