Skip to content

Latest commit

 

History

History
104 lines (84 loc) · 3.56 KB

README.md

File metadata and controls

104 lines (84 loc) · 3.56 KB

Build Status Latest Stable Version Total Downloads License Minimum PHP Version

Date/time converter into "n time ago" format that supports multiple languages. You can easily contribute any language that you wish.

Languages

Default language is English. Optionally you can set the language in your application by calling set() method and passing a flag ru for Russian or en for English language. You can see supported languages in the next section.

Serhii\Ago\Lang::set('ru');

Supported languages

Language Short representation
English en
Russian ru
Ukrainian uk

Usage

For outputting post publishing date or something else you can just pass the date to method trans(). It will count the interval between now and given date and returns needed format. Internally given date will be parsed by strtotime() PHP's internal function.

use Serhii\Ago\TimeAgo;

TimeAgo::trans('now - 10 seconds'); // output: 10 seconds ago

Options

As the seconds argument trans method excepts array of options or single option. Here is an example of passed options.

use Serhii\Ago\Option;
use Serhii\Ago\TimeAgo;

TimeAgo::trans('yesterday'); // output: 1 day ago
TimeAgo::trans('yesterday', Option::NO_SUFFIX); // output: 1 day
TimeAgo::trans('now', Option::ONLINE); // output: online
TimeAgo::trans('now', [Option::ONLINE, Option::UPPER]); // output: ONLINE

Available options

All options are available in Serhii\Ago\Option::class as constants.

Option Description
Option::ONLINE Display "Online" if date interval within 60 seconds. After 60 seconds output will be the same as usually "x time ago" format.
Option::NO_SUFFIX Remove suffix from date and have "5 minutes" instead of "5 minutes ago".
Option::UPCOMING Without this option passed time will be subtracted from current time, but with this option it will take given time and subtract current time. It is useful if you need to display a counter for some date in future.
Option::UPPER Set output to uppercase.

Quick Start

composer require serhii/ago