Home / date / Convert a date / timestamp to time ago for posts
  • How to Convert a date / timestamp to time ago for posts

    convert-a-date-timestamp-to-time-ago-for-posts

    Displaying an exact date on your posts is great but why not change things up just a little and display “1 Hour Ago, 2 Hours Ago, 1 Day Ago, 2 Days Ago, 1 Week Ago, etc. Just add this snippet to your single.php, index.php or any other template within the loop and you are good to go.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago';
    

    ( WordPress codex functions, hooks, in this snippet. )

    get_the_time, the_time, current_time, human_time_diff,
  • maybe you should start thinking about internationalising your snippets, we’re living in the 21st century after all…

    • Lots of things I would like to add to the site, however only so much time in a day. This is a good idea though, thanks.