WordPress.org

Plugin Directory

Test out the new Plugin Directory and let us know what you think.

Relative Date

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

Output Formats

The dates will be shown in one the following formats, depending on relevance...

  • Years, months
  • Years, weeks
  • Months, weeks
  • Months, days
  • Weeks, days
  • Days, hours
  • Hours, minutes
  • Minutes, seconds
  • Seconds

Functions

If you're happier changing your theme then this is definately the best way to do it as you can tweak the output to just how you'd like it! To use, simply call either get_relative_date or relative_date - the first will return the relative date and the second will output it.

No parameters are required and if none are specified then it will simply create a relative date for the current post or page based on how old it is. However, you can specify, as the first or second parameter, a Unix format date (all dates specified must be in Unix format). If one is supplied then the difference will be between that and the current date. If a second date is specified then the output will be the difference between the 2 dates.

In most cases you'll probably use this plugin to display a post time...

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

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 uses 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, a theme may have 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()

Show Relative Dates Automatically

If editing your theme isn't something you're comfortable with then you can achieve an automatic conversion.

Please bear in mind, however, that this will use default depth and divider settings and, depending on how your theme outputs it, the result may not make sense (depending on whether the original call hard-codes any before/after text or passes it as a parameter, as it should do).

Anyway, if you want to try it, head into your Administration screen and select Settings -> General. Near the bottom you should find a new option named "Use Relative Dates". Tick this box and then click on "Save Settings". Your blog dates should now show as relative dates.

Requires: 2.5 or higher
Compatible up to: 4.6.2
Last Updated: 2 months ago
Active Installs: 100+

Ratings

2 out of 5 stars

Support

Got something to say? Need help?

Compatibility

+
=
Not enough data

1 person says it works.
0 people say it's broken.

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