Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Any way to change how the date is displayed with get_comments..?

I have the following..

 <?php
       $comments = get_comments( array(
       'post_id' => $post->ID,
       'number' => 4,
       'status' => 'approve'
       ) );

       foreach($comments as $comment) :
       echo "<li class=clearfix>
       <span class=comment-details>{$comment->comment_content}</span>
       <span class=comment-date>{$comment->comment_date}</span>
       <span class=comment-author>By {$comment->comment_author}</span>

       </li>";
       endforeach;

?>

Comes out with something like.. 2012-02-20 17:09:07 when i just want something like 6th January 2011

share|improve this question
    
Have you checked what settings you've made for time display and such in the back-end/admin UI? –  kaiser Feb 21 '12 at 14:59
add comment

1 Answer

up vote 0 down vote accepted

The way you're doing this, you're going to get the raw SQL date that's stored in the wp_comments table.

You can still use the convenience functions like comment_date with get_comments. In this case, we'll use get_comment_date. The first argument is the date format -- if you leave it blank, WP will use whatever date format is set in the options > general page.

<?php
foreach($comments as $comment) :
   echo "<li class=clearfix>
   <span class=comment-details>{$comment->comment_content}</span>";
   echo '<span class=comment-date>' . get_comment_date( '', $comment ) . '</span>';
   echo "<span class=comment-author>By {$comment->comment_author}</span>

   </li>";
endforeach;

Otherwise you could use PHP's strtotime and date to generate the date format you want.

date('F j, Y', strtotime($comment->comment_date));
share|improve this answer
    
Stunning, thank you :) –  Jezthomp Feb 21 '12 at 15:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.