mcallaro88 Posted March 12, 2012 Share Posted March 12, 2012 Hello. I'm new to pHp and I would like to know how to get my $date_posted to read as March 12, 2012, instead of 2012-12-03. Here is the code: <?php $sql = " SELECT id, title, date_posted, summary FROM blog_posts ORDER BY date_posted ASC LIMIT 10 "; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $id = $row['id']; $title = $row['title']; $date_posted = $row['date_posted']; $summary = $row['summary']; echo "<h3>$title</h3>\n"; echo "<p>$date_posted</p>\n"; echo "<p>$summary</p>\n"; echo "<p><a href=\"post.php?id=$id\" title=\"Read More\">Read More...</a></p>\n"; } ?> I have tried the date() function but it always updates with the current time & date so I'm a little confused on how I get this to work. Quote Link to comment https://forums.phpfreaks.com/topic/258774-how-to-get-a-date-from-a-blog-post-to-read-full-month-date-and-year/ Share on other sites More sharing options...
trq Posted March 12, 2012 Share Posted March 12, 2012 I have tried the date() function but it always updates with the current time & date so I'm a little confused on how I get this to work. How about posting your actual problematic code then? Quote Link to comment https://forums.phpfreaks.com/topic/258774-how-to-get-a-date-from-a-blog-post-to-read-full-month-date-and-year/#findComment-1326574 Share on other sites More sharing options...
mcallaro88 Posted March 12, 2012 Author Share Posted March 12, 2012 thorpe that is my code I don't have a problem with the code. I just don't know how to get the date to display the way I'd like it. Everything works fine and displays properly but I have no clue how to change the time to what I want. Do you know how I could get it to display that way? Quote Link to comment https://forums.phpfreaks.com/topic/258774-how-to-get-a-date-from-a-blog-post-to-read-full-month-date-and-year/#findComment-1326576 Share on other sites More sharing options...
timmah1 Posted March 12, 2012 Share Posted March 12, 2012 while($row = mysql_fetch_assoc($result)) { $id = $row['id']; $title = $row['title']; $date_posted = date("F j, Y", strtotime($row['date_posted'])); $summary = $row['summary']; Quote Link to comment https://forums.phpfreaks.com/topic/258774-how-to-get-a-date-from-a-blog-post-to-read-full-month-date-and-year/#findComment-1326583 Share on other sites More sharing options...
mcallaro88 Posted March 12, 2012 Author Share Posted March 12, 2012 Thanks timmah1! Quote Link to comment https://forums.phpfreaks.com/topic/258774-how-to-get-a-date-from-a-blog-post-to-read-full-month-date-and-year/#findComment-1326588 Share on other sites More sharing options...
Pikachu2000 Posted March 12, 2012 Share Posted March 12, 2012 $sql = " SELECT id, title, DATE_FORMAT(date_posted, '%M %e, %Y') AS date_posted, summary FROM blog_posts ORDER BY date_posted ASC LIMIT 10"; Quote Link to comment https://forums.phpfreaks.com/topic/258774-how-to-get-a-date-from-a-blog-post-to-read-full-month-date-and-year/#findComment-1326590 Share on other sites More sharing options...
xyph Posted March 12, 2012 Share Posted March 12, 2012 timmah1's solution is redundant. Use MySQL's date and time functions instead. Specifically, the date_format function. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format SELECT id, title, DATE_FORMAT(date_posted,'%M %e, %Y') as date_posted, summary FROM blog_posts ORDER BY date_posted ASC LIMIT 10 [edit] Pikachu beat me [/edit] Quote Link to comment https://forums.phpfreaks.com/topic/258774-how-to-get-a-date-from-a-blog-post-to-read-full-month-date-and-year/#findComment-1326591 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.