3

I see that theres no way to get a formatted date from a function like

echo get_the_date($post->ID, 'd M Y');

get_the_date only works with the current $post. If I used get_posts() how can I format the date. With pure PHP I can do something like ...

echo DateTime::createFromFormat('Y-m-d h:i:s', $cp->post_date)->format('d M Y');

but its abit long, and I don't know if there's a need to actually create a DateTime object, perhaps its a waste of resources?

1 Answer 1

7

echo date('Y-m-d h:i:s', strtotime($cp->post_date)); ... or better use the wordpress functionado echo mysql2date('Y-m-d h:i:s', $cp->post_date);

2
  • 1
    +1 for mysql2date(), it's how get_the_date() does it internally.
    – Rarst
    Feb 20, 2011 at 11:25
  • 4
    <?php echo mysql2date( get_option('date_format'), $cp->post_date ); ?> to use the configuration date set in wordpress admin Jul 30, 2012 at 21:30

Your Answer

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

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