
Converting WordPress Posts Timestamp to ‘Time Ago’ Format
- October 26, 2016
- Leave a comment
WordPress by default provides a way to change your WordPress date format or time format. You can change it from the general settings of your WordPress Dashboard.
Sometimes, you may require “time ago” format on your website, same as followed by Twitter and Facebook. WordPress provides certain functions i.e. the_date() and the_time() which are used to display or return date or time on the posts/pages in the following sample formats:
- “1 Hour Ago”
- “2 Hours Ago”
- “1 Day Ago”
- “2 Days Ago”
- “1 Week Ago”
- “2 Weeks Ago”
- “1 Month Ago”
- “2 Months Ago” and so forth
To achieve this functionality, use the following code within the loop or in any template of your WordPress theme:
1 2 3 4 |
<?php // display time ago format echo human_time_diff( get_the_time('U'), current_time('timestamp') ).' ago; ?> |
Simply remember to include this code somewhere in the functions.php file of your Theme.
User Comments