WordPress.org

Ready to get started?Download WordPress

Plugin Directory

Artiss Relative Date

Display a relative date (e.g. "4 days ago")

Using the Functions

All dates specified must be in Unix format. In most cases you'll probably use this plugin to display a post time. That is returned by the code get_the_time(). By specifying get_the_time('U') instead, it will return it in Unix format. Don't forget, however, that if you don't specify a date then the post time will be assumed anyway. None-the-less, we'll use this date as an example.

So, to return the relative date instead you'd specify get_relative_date( get_the_time( 'U' ) ). Put that together along with an appropriate script to check that the plugin is active, you might have something like this in your theme...

if ( function_exists( 'get_relative_date' ) ) {
    echo 'Created ' . get_relative_date( get_the_time( 'U' ) ) . ' ago.';
}

If you need to convert other dates/times to Unix format, you may want to look at the PHP commands date and strtotime.

Additional Parameters

There is another parameter that you can use, if you wish to override the default divider (which is a comma). For example...

get_relative_date( get_the_time( 'U' ) )

Might produce a result of 3 days, 4 hours ago. Whereas...

get_relative_date( get_the_time( 'U' ), ' and ' )

Would produce a result of 3 days and 4 hours ago. Note that you need to specify any spaces as well around the divider that you need.

By default, all output has a maximum depth of 2 values - for example, 3 days, 4 hours ago. If you specify a number of 1 or 2 as a parameter, however, you can control this depth. A depth of 1 with the previous example would output 3 days ago.

With regard to the sequence of the parameters, it doesn't matter - you can specify the parameters in any order (up to 2 dates, 1 divider and 1 depth).

Adding to Your Theme

If you wish to change your theme so that it used a relative date then you'll need to modify your theme files, for example single.php. Most themes will use get_the_date, get_the_time, the_date or the_time to output dates on a blog.

As an example, my theme has the following...

the_time( get_option( 'date_format' ) );

This is getting the default WordPress data format and then outputting the current posts' created date in this format. To convert to a relative date, you'd replace this with...

relative_date( get_the_time( 'U' ) )

In fact, leaving the parameter out will produce the same result, as this plugin will assume the date of the current plugin if nothing is specified. In this case, we're passing to the plugin the date of the current post in Unix format (which is the required format for this plugin).

Licence

This WordPress plugin is licensed under the GPLv2 (or later).

Support

All of my plugins are supported via my website.

Please feel free to visit the site for plugin updates and development news - either visit the site regularly, follow my news feed or follow me on Twitter (@artiss_tech).

For problems, suggestions or enhancements for this plugin, there is a dedicated page and a forum. The dedicated page will also list any known issues and planned enhancements.

Requires: 2.5 or higher
Compatible up to: 3.4.2
Last Updated: 2012-9-27
Downloads: 2,010

Ratings

3 stars
3.2 out of 5 stars

Support

Got something to say? Need help?

Compatibility

+
=
Not enough data

0 people say it works.
0 people say it's broken.

100,1,1
100,1,1
0,2,0