Outdated version
You are currently viewing an outdated version of the Ago library. Switch to the latest version to access the newest features, improvements, and updates.
Get Started β
Date/time converter into "n time ago" format that supports multiple languages. You can contribute any language that you wish easily by creating a pull request. I would gladly merge it in if you follow the simple steps.
This library is well tested, optimized and already used in many production apps. It has shown itself pretty well. If you find any issues or bugs π, please create an issue, and I'll fix it as soon as I can.
Quick Start β
To use Ago, first, you need to install it via Composer package manager and have minimum PHP version 7.1
.
composer require serhii/ago:^3.2
Supported PHP versions β
Ago tries to support as many PHP versions as possible. Here is the list of supported PHP versions:
- β 7.1
- β 7.2
- β 7.3
- β 7.4
- β 8.0
- β 8.1
- β 8.2
- β 8.3
- β 8.4
Supported languages β
Flag | Language | Code (ISO 639-1) |
---|---|---|
π¬π§ | English | en |
π·πΊ | Russian | ru |
πΊπ¦ | Ukrainian | uk |
π³π± | Dutch | nl |
π©πͺ | German | de |
Keep updated
Make sure to update the library to the latest version to get the latest languages support.
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. The methods excepts a timestamp, date string, Carbon instance or DateTime.
use Serhii\Ago\TimeAgo;
use DateTimeImmutable;
use Carbon\Carbon;
use DateTime;
echo TimeAgo::trans('-10 seconds'); // "10 seconds ago"
echo TimeAgo::trans(time() - 86400); // "1 day ago"
echo TimeAgo::trans(Carbon::now()->subDay()); // "1 day ago"
echo TimeAgo::trans(CarbonImmutable::now()->subDay()); // "1 day ago"
echo TimeAgo::trans(new DateTime('-5 minutes')); // "5 minutes ago"
echo TimeAgo::trans(new DateTimeImmutable('-5 minutes')); // "5 minutes ago"
When you pass the date in the future, it will output the interval to this date. It's very convenient, because you can pass almost any date format and it will give you the correct output.
echo TimeAgo::trans(time() + 86400); // "1 day"
echo TimeAgo::trans('+10 minutes'); // "10 minutes"
Version 2.2.0
If you use version less than 2.2.0
, TimeAgo::trans()
method will except only a string type.