WordPress actually has an obscure and little known function called human_time_diff()
that does this. It takes two arguments; the first is the earlier timestamp, and the second is the later timestamp. Both should be Unix timestamps. The first argument is required, but the second is optional, and will use time()
if left empty. So, for example, in the loop, you could do this:
<p>Posted <?php echo human_time_diff( get_the_time( 'U' ) ); ?> ago.</p>
The function will only do minutes, hours, and days, though. If you need to get weeks, for example, you could do something like this:
$diff = explode( ' ', human_time_diff( get_the_time( 'U' ) ) );
if( $diff[1] == 'days' && 7 <= $diff[0] ){
$diff[1] = 'week';
$diff[0] = round( (int)$diff[0] / 7 );
if( $diff[0] > 1 )
$diff[1] .= 's';
$diff = implode( ' ', $diff );
}
That would give you N week(s)
as a string.
Related Posts:
- Set Expiration Date of a Post from the Frontend with wp_insert_post
- How can I include a post in a theme?
- Adding “latest from the blog” to the homepage
- When to use add_action(‘init’) vs add_action(‘wp_enqueue_scripts’)
- Change admin bar to default:off
- How to add Color Picker in Theme Options for the following?
- Template for individual post designs
- How to know if get_posts() failed?
- Theme Review: post thumbnail, header image, content width
- How to determine if a category is empty?
- The seventh parameter passed to add_submenu_page()
- What would happen if the admin installs a plugin when the plugin is included in the theme?
- How do I get my child-theme to work with my theme’s includes folder?
- wp_insert_post breaks rewrite rules
- Where can I find a good reviewed collection of Twenty Ten child themes?
- how to create theme based widget that can be drop in sider bar or footer
- CSS in child theme not overriding the parent theme [closed]
- Template Hierarchy for get_header()
- How to add (css) classes to only one wp_nav_menu()?
- Remove frameborder attribute from iframes
- Multiple Content Block
- How to add suggest plugin to theme?
- how do I get a sidebar’s id or number for use with is_active_sidebar()
- Looking for the code in twentyten that allows users to select images for the header/banner
- Any tag pagination page (except the 1st page) loads index.php template instead of tag.php
- Adding LazyLoad and InfiniteScroll to WordPress Theme
- WordPress default theme css units
- How to remove class attributes from custom menu markup?
- If I build a custom theme, will it update?
- Implement advanced search
- Implement content-nosidebar.php / content-leftsidebar.php
- Where should I update_options in a theme?
- Users table association with users_metadata table appears broken
- multisite: global menu containing network site links?
- Injecting a custom sql query into a page/theme
- What are the Entry classes for?
- Using an if statement in the theme customizer page
- Installing WordPress locally
- Custom markup with wp_nav_menu based on pages
- How to edit my theme for full width?
- Force Balance Tags not working
- Use second time navigation.js in underscores [closed]
- Fetch wordpress video attachment with post ID
- Template selection area is not visible on page editor sidebar in wordpress twentty sixteen default theme
- Why does has_nav_menu return false when get_registered_nav_menus shows the opposite?
- Displaying icon image for WordPress post formats, is there a cleaner way to do this?
- Overrite parent functions using child functions
- How can I develop multiple widgets for a WordPress theme?
- WordPress 3 botches my Custom Meta Box values on Save
- How to seperate static phrases from template files in custom theme?
- Weirdness in jQuery supplied with WordPress
- How can I compare publish and update dates for a single post?
- How to code custom special page
- Displaying the right content on a page url
- Get Post’s Excerpt Without the Wrapping tags and the Read More link
- Is it possible to set a variable for get_post_meta?
- Custom admin logo not showing after wordpress 4.5 upgrade
- Unable to change the priority with ‘remove_action’ and ‘add_action’ in child theme
- Translate a child theme with pure PHP and gettext
- Injecting pre-defined text from custom page template to editor
- Remove h1 from 2015 theme
- Only the latest post shows up on post page?
- WordPress pulling in random page themes
- Trouble in enquing all js files under certain directory
- Cant’ Grab WordPress Built-in Posts Through Loop
- Can’t change theme name
- multiple queries to get posts from same category?
- Stop WordPress from showing images on non post pages
- Parent/Child pages
- enqueuing external and internal js and css in wordpress did not work with owl.js animate.css
- Wrong activation/confirmation link in email
- Next and previous post link shows error in first and last post
- Disabling wordpress added class
- Problem with pagination link (error 404)
- WordPress wp_get_current_user returning blank values until refresh
- use a single nonce in three different nonce field
- Whether an tag is required in header?
- Theme is Enqueueing Everything in Footer
- Local theme changes upload on server but theme changes not showing
- Media & Plugin screens stall
- wp_kses allow checkbox class and checked
- Is wp_mail plugin territory?
- HTML TO WP Theme : Submenu goes down wp_nav_menu
- New created Theme leads to malfunctioning Xampp Apache –> functions.php is cause
- Understanding WordPress theme files and underscores
- New theme HTML5 Support for Search in WordPress 4.4
- How do I remove p tag *insertions*? Disabling `wpautop` removes manual tags
- Create a variable with string, array or multiple values
- Woocommerce Theme Reviews / Comments
- What hook to use for loading a custom class extension during Theme initialization?
- Moving Posts and Users Only to New Site
- What are these variables and where are they documented?
- Use external fonts in WordPress stylesheet
- Custom link color or stylesheets
- What is the advantage of using home.php over index.php for the front page
- Why do some sites show themes/”themename” as the only theme?
- How can i move my product name & price from below thumbnail to be the rollover content in Avada & Woocommerce?
- Feeds are showing where Post archive page should be
- WordPress Two Level Filters on Getting Custom Taxonomy Terms
- I want to resize post featured image without cropping image. How?