So here is my code to display the post created date:

    $date_create = '<div class="mydate-created">';
    $date_create .= get_the_date('',$product->id);
    $date_create .='</div>';
    $output .= '</div>';
    $output .= '<div class="mydate-created">';
    $output .= get_the_date('',$product->id);
    $output .= '</div>';

It displays the date of post created as the following "2015-02-23"

I know there is setting in the backend to change the date displayed.

However is there a way to display it like "1 min ago" or " 1 hour ago"?



Here you go. This goes in functions.php

This changes the time on your website everywhere. Now you can keep using <?php echo get_the_date(); ?> in your loop or theme files.

// Relative date & time
function wpse_relative_date() {
  return human_time_diff( get_the_time('U'), current_time( 'timestamp' ) ) . ' ago';
add_filter( 'get_the_date', 'wpse_relative_date' ); // for posts and pages
// add_filter( 'get_comment_date', 'wpse_relative_date' ); // for comments

I added a PHP comment before add_filter to also change comment dates too. Because I am not sure if you want that for comments too. Remove PHP comment if needed.

| improve this answer | |
  • @steveKim If this is the answer you were searching for, please mark it as solution by ticking the checkmark below the voting arrows on the left of this answer. – kaiser Feb 25 '15 at 9:57
  • Thank you. It was. I didn't know about the check mark. Thank you Robert! =) – Steve Kim Feb 25 '15 at 10:02
  • :) I am glad I could help you with this. – Robert hue Feb 25 '15 at 10:03

You can use <?php human_time_diff( $from, $to ); ?> to return a human readable time difference.

Take a look at the Wordpress Codex Function Reference/human time diff.

| improve this answer | |
  • Edited and code included. – fructurj Feb 25 '15 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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