Pretty Date


How can get the date post meta to display in a human readable format i.e "Posted: 3 Hours ago" Yesterday, 2 months ago etc?

Thank you for all your help


Hey Chenje,

There is WordPress Core function human_time_diff() (see documentation here) which you can use for that. You can use Beans output filters to change the prefix and date format as follow:

// Change the post meta date prefix.
add_filter( 'beans_post_meta_date_prefix_output', 'example_post_meta_date_prefix' );

function example_post_meta_date_prefix() {

 echo 'Posted: ';

}

// Change the post meta date/time to human difference format.
add_filter( 'beans_post_meta_date_text_output', 'example_post_meta_date_text' );

function example_post_meta_date_text() {

  echo human_time_diff( get_the_time( 'U' ) ) . ' ago';

}

This snippet goes in your child theme functions.php or in your template modifications files depending how your child theme is structured.

Happy coding,

Write a reply

Login or register to write a reply, it's free!