WordPress Date & Time Tutorial For Beginners

In this article I’m going to share some tips for displaying date and time on WordPress posts and comments information.

Though I’m 99.9% sure, I recommend you to take backup of your theme files or at least the required theme file. Please!

Display “Time Ago” for Posts or Comments

Just copy and paste the following code in functions.php file.

function time_ago( $type = 'post' ) {
$d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';
return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');
}

This uses built-in WordPress function human_time_diff(). Now place the following PHP code on single.php file or comments.php.

Note : File names may vary according to themes.

<?php echo time_ago(); ?>


Display Copyright Year

We don’t need to add copyright date or time, displaying the year is more than enough.

Add the following code in footer.php (most preferred area).

Coding :

Copyright © <?php the_time('Y'); ?> < ?php bloginfo('name'); ?>

Output :

Copyright © 2009 HellBound Bloggers

Tip : Update The Copyright Year Automatically


Adding the Date and the Time to your posts

You can add date and time to your posts in this format easily.

This entry was posted on <?php the_time('l, F jS, Y') ?> at < ?php the_time('g:i a'); ?> and is filed under <?php the_category(', ') ?>

Output :

This entry was posted on 21 July 2010 at 06:00 PM and is filed under Resources


Display the Date and the Time your post was modified

The template tag the_modified_date shows the date your post was last modified. Similarly the_modified_time() displays the last modified time.

Note : These only work when used within the WordPress Loop.

Coding :

This post was published on <?php the_date('F j, Y'); ?> and was last modified on < ?php the_modified_date(); ?> at <?php the_modified_time() ?>

Output :

This post was published on July 18, 2010 and was last modified on July 20, 2010 at 5:30 pm

For further formatting and customizing, check WordPress Codex.

Having any problems? Do you know any other trick? Please share it in the comments!

13 thoughts on “WordPress Date & Time Tutorial For Beginners”

  1. How Can I change date to previous date , Suppose my Old Blog is deleted and I want to post same post as I posted before .. I want the same date on that time

    Reply
  2. Thanks for sharing this, but I have to backup first before integrating this like what you said. What other tips do you have for WP beginners? 🙂

    Reply
  3. Thanks for sharing this Pradeep, like date and time to be display on my blog, I am going to try the code when I have time to do this.

    Reply
  4. I don’t understand what will appear in the display “Time Ago” function. It will display hours ago or days ago?

    Reply

Leave a Comment