Jump to content

How to get a date from a blog post to read Full month, Date, and Year?


mcallaro88

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.