Skip to content

groenewege/relative-date

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Relative Date for Kirby CMS

Release Issues License Moral License

The Relative Date plugin for Kirby CMS displays date and time to a human-readable relative format. It converts your absolute date (and time) in something relative and more readable, e.g.:

2 months 3 days ago
5 hours 47 minutes 18 seconds from now

The plugin is free. However, I would really appreciate if you could support me with a moral license!

Overview

  1. Installation
  2. Usage
  3. Options
  4. Help & Improve
  5. Languages
  6. Known Issues)
  7. Version History

Installation

  1. Download the current release.
  2. Copy everything to site/plugins/relative-date/.

To update to a higher version of this plugin, replace the files with the newer version.

Usage

You can either use it as field method:

<?php echo $page->published()->relative() ?>

Or as Kirbytext tag:

Published: (relativedate: 2015-02-15)

Or with the relativeDate() helper function:

<?php echo relativeDate($page->modified()) ?>

Options

Parameters

Language
You can set some options on an per-use basis by passing them through as parameters. If you wan to get a specific language, just pass the locale as string:

$page->published()->relative('es');

Length
If you want to specify the number of elements, just pass the (length)[#length] as integer:

$page->published()->relative(1);

Multiple parameters
If you want to specify multiple options, pass them as an array:

$args = array(
  'lang'      => 'es',
  'length'    => 1,
  'threshold' => 999999,
  'fuzzy'     => false
);
$page->published()->relative($args);

In a Kirbytag
If you use the Kirbytext tag, this works as well:

(relativedate: 2015-02-15 lang: es length: 1 threshold: 99999 fuzzy: false)

Global options

In addition to passing some options as parameters, you also can set some global options in your sites/config/config.php:

Threshold
Sometimes you only want relative dates for dates that are not too far in the past/future, but not for dates really far away. In that case you can set a threshold (in seconds). Only dates on the range of that threshold from the current time will be displayed as relative dates:

c::set('relativedate.threshold', 604800);

Length
You can define how many date/time elements the phrase should entail. The default is 2 elements (e.g. '1 year 4 months' or '2 weeks 3 days' or '2 hours 34 minutes'). You can set your own phrase length in two ways:

c::set('relativedate.length', 4);

Conjunctions
You can glue the date and time elements together with conjunctions instead of just spaces by either setting the option to true (relying on language files) or specifying a conjunction:

c::set('relativedate.conjunction', 'et');

Fuzzy
Relative Date supports fuzzy expressions, which means that instead of the rather exact date difference one of the following expressions will be displayed: today, tomorrow/yesterday, next/last {weekday}, next/last week, next/last month:

c::set('relativedate.fuzzy', false);

Fuzzy expressions are only supported by a few of the included languages yet (English, Brazilian Portuguese, Catalan, French, German, Indonesian, Spanish, Swedish).

Default Language
You can also define the default fallback language (if not, it defaults to English):

c::set('relativedate.lang', 'ja');

Help & Improve

Help is always appreciated. Suggestions, ideas or bugs - let me please know by opening an issue.

In addition, if you think a language is missing, let me know. And if you can even help with translations, head over here.

Languages supported

  • Arabic (ar)
  • Bulgarian (bg)
  • Catalan (ca)
  • Chinese (zh)
  • Chinese Taiwan (zh_TW)
  • Czech (cs)
  • Danish (da)
  • Dutch (nl)
  • English (en) [default]
  • Finnish (fi)
  • French (fr)
  • German (de)
  • Indonesian (id)
  • Italian (it)
  • Japanese (ja)
  • Norwegian (no)
  • Polish (pol)
  • Portuguese (pt)
  • Portuguese Brazilian (pt_BR)
  • Romanian (ro)
  • Russian (ru)
  • Serbian (sr)
  • Spanish (es)
  • Swedish (sv)
  • Thai (th)
  • Turkish (tr)

Known Issues

  • This plugin cannot be used with the Kirby's default date field as it is not chainable. Please use the plugin on a field named differently than date, e.g. published. As another workaround the date field could be passed directly to the relativeDate() helper function:
<?php echo relativeDate($page->date('Y-m-d')) ?>

Version History

Check out the more or less complete changelog.

About

Relative Date and Time (multi-language) plugin for Kirby 2 CMS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%