Plugin for Kirby 2 CMS that coverts date and time to a human-readable relative format: 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
- Download the current release.
- Add the
relative-date.phpandlangfolder to thesite/plugins/relative-date/directory. You probably need to create therelative-datefolder. - Then use it on any date field, e.g.:
$page->published()->relative()To update to a higher version just replace the same files by their newer version.
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.
Globally in your sites/config/config.php:
c::set('relativedate.threshold', 604800);On a per case basis:
$page->published()->relative(604800);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:
Globally in your sites/config/config.php:
c::set('relativedate.length', 4);On a per case basis:
$page->published()->relative($threshold, 4);You can also define the default fallback language (if not, it's English) in your sites/config/config.php:
c::set('relativedate.default', 'ja');There is a way to get more fuzzy expressions, e.g. yesterdayinstead of 17 hours 40 minutes ago. For that you need to set up the fuzzy expressions, which you want to use, in your site/config/config.php, e.g.:
c::set('relativedate.fuzzy', array(
/* English (en) */
'en' => array(
'tomorrow' => '/^[1-2]?[1-9] hour(s)?(.*)/',
'yesterday' => '/^(1 day(.*)|[1-2]?[1-9] hour(s)?(.*))/',
),
));The config item consists of an array in which each language gets its own array of fuzzy expressions. Each fuzzy expression consists of a key-value pair. They key represents the fuzzy term that you wanna have in your displayed result at the end, the value containts a regular expression of what is to replace.
You can find a collection of fuzzy expression rules for different languages in the fuzzy-examples.phpfile.
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 the translation, please provide the following information:
- Words (singular & plural) for second, minute, hour, day, week, month and year
- Terms that express A) that date & time are in the future (e.g. "1 hour from now") and B) that date & time are in the past (e.g. "3 days ago")
- Where to put these terms in relation to the date/time-phrase (before, after, in between?)
- English (en) [default]
- Arabic (ar) [experimental]
- Bulgarian (bg)
- Chinese (zh)
- Chinese Taiwan (zh_TW)
- Czech (cs)
- Danish (da)
- Dutch (nl)
- Finnish (fi)
- French (fr)
- German (de)
- Italian (it)
- Japanese (ja)
- Norwegian (no)
- Polish (pol)
- Portuguese (pt)
- Portuguese Brazilian (pt_BR)
- Russian (ru)
- Serbian (sr)
- Spanish (es)
- Swedish (sv)
- Thai (th)
- Turkish (tr)
Credits go to the Laravel Date project for their languages variables as well as this localization guide for providing the plural set rules.
v0.9
- Added threshold option
- Complete rewrite fuzzy expression logic, located it to
site/config/config.php - Added Czech, Bulgarian, Chinese, Chinese Taiwan, Danish support
- Improved minor things on Thai translation
v0.8
- Complete rewrite of logic for languages that feature multiple plural forms with specific rule sets (e.g. Russian)
- Fixed Russian localization
- Added support for Norwegian, Polish, Brazilian Portuguese, Finnish, Turkish, Serbian
- Added experimental support for Arabic
v0.7
- Added basic fuzzy expression support
- Fixed wrong language matching for years
- Code simplifications