1

I'm using the following

<?php
function custom_echo($x)
{
  if(strlen($x)<=150)
  {
    echo $x;
  }
  else
  {
    $y=substr($x,0,150) . '...';
    echo $y;
  }
}

// Include the wp-load'er
include('../../blog/wp-load.php');

// Get the last 10 posts
// Returns posts as arrays instead of get_posts' objects
$recent_posts = wp_get_recent_posts(array(
  'numberposts' => 4
));

// Do something with them
echo '<div>';
foreach($recent_posts as $post) {
  echo '<a class="blog-title" href="', get_permalink($post['ID']), '">', $post['post_title'], '</a><br />', $post['post_date'], custom_echo($post['post_content']), '<br /><br />';
}
echo '</div>';
?>

What I'm having problems with is the $post['post_date'] - it comes out as 2012-12-03 13:59:56 - I just want this to read December 3, 2012. I have no idea how to go about it. I know there are some other solutions that are similar to this, but I'm new to this and genuinely didn't understand them...?

Help?

Thanks.

0

1 Answer 1

8

In PHP the date() function comes with a lot of formatting possibilities. What you want to do is use this statement:

echo date("F j, Y", $post['post_date']);

Here

  1. 'F' corresponds to a full textual representation of a month, such as January or March
  2. 'j' corresponds to a Day of the month without leading zeros
  3. 'Y' corresponds to a A full numeric representation of a year, 4 digits

You can find more information and format on the documentation here : http://php.net/manual/en/function.date.php

EDIT: If your variable $post['post_date']contains an existing date you should do this instead:

echo date("F j, Y", strtomtime($post['post_date']));

The function strtotime() will first convert your existing date in a timestamp for date() to work properly.

More info on strtotime() here : http://php.net/manual/en/function.strtotime.php

Sign up to request clarification or add additional context in comments.

4 Comments

That's interesting - I saw that documentation but wasn't sure how to implement it. I tried that statment, but got January 1, 1970 for all blog entries?
@user1802256: Try doing echo date("F j, Y", strtotime($post['post_date']));
Yep @RocketHazmat is right I was editing my post to reflect this.
That worked! Thank you! This worked as well: mysql2date('F j, Y', $post['post_date']) What's the difference between the two?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.