PHPackages                             dotzero/laravel-localized-carbon - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. dotzero/laravel-localized-carbon

AbandonedArchivedLibrary

dotzero/laravel-localized-carbon
================================

A localizable version of Carbon for Laravel 5

v1.2.0(6y ago)0801MITPHPPHP ^7.1.3

Since Apr 5Pushed 6y ago1 watchersCompare

[ Source](https://github.com/zero-archive/laravel-localized-carbon)[ Packagist](https://packagist.org/packages/dotzero/laravel-localized-carbon)[ RSS](/packages/dotzero-laravel-localized-carbon/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (6)Versions (4)Used By (0)

A localizable version of Carbon for Laravel 5
=============================================

[](#a-localizable-version-of-carbon-for-laravel-5)

[![Build Status](https://camo.githubusercontent.com/65b9b06d7f1800688046adc33eda7917d89303d5d3860299a137da028e63ccef/68747470733a2f2f7472617669732d63692e6f72672f646f747a65726f2f6c61726176656c2d6c6f63616c697a65642d636172626f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/dotzero/laravel-localized-carbon)[![Latest Stable Version](https://camo.githubusercontent.com/fb4248a9cff63b6baf6b40848661987465783c88aadc3a378f8fe85935e1a332/68747470733a2f2f706f7365722e707567782e6f72672f646f747a65726f2f6c61726176656c2d6c6f63616c697a65642d636172626f6e2f76657273696f6e)](https://packagist.org/packages/dotzero/laravel-localized-carbon)[![License](https://camo.githubusercontent.com/78ce35b948a563465d1d023378de7255ea0a26b3d10461c87440f8b0b5548713/68747470733a2f2f706f7365722e707567782e6f72672f646f747a65726f2f6c61726176656c2d6c6f63616c697a65642d636172626f6e2f6c6963656e7365)](https://packagist.org/packages/dotzero/laravel-localized-carbon)

Localized Carbon is an extension of a popular Carbon package, designed specially for Laravel 5 framework. By localization I mean its `diffForHumans` function, which returns a human-readable string of time interval. This package also supports genitive months by introducing the `%f` key in `formatLocalized` method.

Installation
------------

[](#installation)

Require this package with [Composer](https://getcomposer.org/) using the following command:

```
$ composer require dotzero/laravel-localized-carbon
```

Next, add package's Service Provider to `app/config/app.php` in `providers` section:

```
'providers' => [
    // ...
    Laravelrus\LocalizedCarbon\LocalizedCarbonServiceProvider::class,
],
```

After that you may want to add some Aliases (`aliases` section of the same config):

```
'aliases' => [
    // ...
    'LocalizedCarbon' => Laravelrus\LocalizedCarbon\LocalizedCarbon::class,
    'DiffFormatter'   => Laravelrus\LocalizedCarbon\DiffFactoryFacade::class,
],
```

Run the command below to publish the lang files into `resources/lang`:

```
$ php artisan vendor:publish
```

Usage
-----

[](#usage)

Note that `DiffFormatter` will only be used for extending default localizations.

If you want to use the power of `LocalizedCarbon` the same way as you did with original `Carbon` in your models, you may want to use supplied trait for this in your models:

```
use \Laravelrus\LocalizedCarbon\Traits\LocalizedEloquentTrait;
```

In this case `LocalizedCarbon` will be used for all dates in your Eloquent model instead of original `Carbon`.

This package provides a `LocalizedCarbon` class which inherits original Carbon, so its usage is absolutely the same as original Carbon's.

But imagine you have a `Comment` model for example, which has default timestamp fields (`created_at` and `updated_at`). You want to display, how much time has gone since its `created_at` in a human-readable format. One way to achieve may be such (in your Blade template):

```
{{ LocalizedCarbon::instance($comment->created_at)->diffForHumans() }}

```

In this case the class will output something like "5 minutes ago". Note that for just an English version of the string original Carbon would be enough. This `LocalizedCarbon` is used to display the message in the current application language. For example, for Russian language it will display "5 минут назад".

But also, you may substitute Laravel's Eloquent model, so the timestamps would be converted to `LocalizedCarbon` instead of original `Carbon`. So the usage could be as if your were using original Carbon:

```
{{ $comment->created_at->diffForHumans() }}

```

As in original Carbon, `diffForHumans` functions has an optional first argument (which is another Carbon instance). It specified the time to which difference should be calculated. By default (a missing or `null` value) current time is used.

Also `LocalizedCarbon` adds an optional second argument, in which you may specify the desired language, or directly a `formatter` class which is used to format the difference-string. By default current application language is used. Also you may specify a Closure in the second parameter which will do formatting. For its signature refer to extending Localized Carbon section.

Supported languages
-------------------

[](#supported-languages)

Current version of Localized Carbon ships with these localizations:

- English (en) (full)
- Russian (ru) (full)
- Ukrainian (uk) (full)
- Dutch (nl) (no genitive)
- Spanish (es) (full)
- Portuguese (pt) (no genitive)
- French (fr) (no genitive)
- Bulgarian (bg) (no genitive)
- Slovakian (sk) (no genitive)
- Turkish (tr) (no genitive)
- Arabic (ar) (no genitive)
- Japanese (ja) (full)
- Bengali (bn) (full)
- Persian (fa) (full)

Extending Localized Carbon
--------------------------

[](#extending-localized-carbon)

If needed localization is not shipped with this package, you may write your own and extend Localized Carbon with it, not even touching the vendor folder itself.

There are a couple of ways to extend Localized Carbon.

First, you may write your own `DiffFormatter` class for this, implementing `Laravelrus\LocalizedCarbon\DiffFormatters\DiffFormatterInterface`. This interface forces the class to have a single `format` method which looks like this:

```
public function format($isNow, $isFuture, $delta, $unit);
```

`$isNow` is a boolean, which is `true` when the time difference is calculated relevant to current time. `$isFuture` is boolean, which is `true` if the DateTime object is in the future relevant to comparable time. `$delta` is an integer, equals to number of units of difference. `$unit` is a time-"unit". It can be either: `second`, `minute`, `hour`, `day`, `week`, `month` or `year`.

So, your `format` method should return a string based on this arguments. As an example see an existing DiffFormatters in `vendor\laravelrus\localized-carbon\src\Laravelrus\LocalizedCarbon\DiffFormatters` directory. You can also reference a lang-files, using `Lang::choice` as it is done in Russian localization for example.

When your class is ready, you must register it within the Localized Carbon. For this you must call `DiffFormatter::extend`method from within any file which is loaded by the framework. For example, you can do it somewhere in `app/start/global.php`.

The `extend` method expects two parameters: first is the language you want to be supported (most often it would be `App::getLocale()`if you want just to use application's language). Next is the instance of your formatter, OR just a name of the class if it can be autoloaded. Consider these examples:

```
$formatter = new Acme\DiffFormatters\FrDiffFormatter;
DiffFormatter::extend('fr', $formatter);

// or

DiffFormatter::extend('fr', 'Acme\\DiffFormatters\\FrDiffFormatter');
```

In the latter case the formatter will be autoloaded when it is needed using IoC. Also note that formatter is loaded only once during application life-cycle due to optimization considerations.

The second way to extend is to pass a Closure as the second parameter. It must have the same signature as the `format` method of `DiffFormatterInterface` interface. For example:

```
DiffFormatter::extend('fr', function($isNow, $isFuture, $delta, $unit) {
    return 'Some formatter diff string!';
});
```

Also, there is a possibility to add an alias for an existing language. For example, Localized Carbon is shipped with Ukranian localization, which is recognized by `uk` language key. But what if your application uses `ua` or `ukr` language, which still means it is Ukranian? In this case you may add an alias for `uk` language in this way:

```
DiffFormatter::alias('ukr', 'uk');
```

Contributing
------------

[](#contributing)

If you've written a formatter for the language which is not supported by current version of Localized Carbon out of the box - feel free to make a pull request with it in the current version branch (1.3), but be sure to adjust your formatter for been used by the package.

The formatter should lie in `src/Laravelrus/LocalizedCarbon/DiffFormatters` directory, following a simple naming convention: the class name should start with the desired language in lower-case, but the first letter in upper-case. The rest part of the name should be "DiffFormatter". The file name should correspond to the class name.

For example, the formatter for `fr` language would lie in `src/Laravelrus/LocalizedCarbon/DiffFormatters/FrDiffFormatter.php`, and the class name would be `FrDiffFormatter`.

Also I need the help of the community to complete the list of genitives for all supported languages. If you know a language and it uses genitives in dates, feel free to contribute. See an example of Russian or Ukranian `lang\XX\months.php` files.

License
-------

[](#license)

A localizable version of Carbon is licensed under MIT:

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 56.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~239 days

Total

3

Last Release

2478d ago

PHP version history (3 changes)v1.0.0PHP &gt;=5.6.4

v1.1.0PHP &gt;=7.0

v1.2.0PHP ^7.1.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/acb949df784d335647f74ca98bf03212a11f4f28ee459443aa599335de418c29?d=identicon)[dotzero](/maintainers/dotzero)

---

Top Contributors

[![Amegatron](https://avatars.githubusercontent.com/u/6664497?v=4)](https://github.com/Amegatron "Amegatron (99 commits)")[![dotzero](https://avatars.githubusercontent.com/u/265633?v=4)](https://github.com/dotzero "dotzero (19 commits)")[![greabock](https://avatars.githubusercontent.com/u/7137261?v=4)](https://github.com/greabock "greabock (8 commits)")[![judantus](https://avatars.githubusercontent.com/u/7925853?v=4)](https://github.com/judantus "judantus (5 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (4 commits)")[![hgeorgilas](https://avatars.githubusercontent.com/u/9117904?v=4)](https://github.com/hgeorgilas "hgeorgilas (4 commits)")[![yuks](https://avatars.githubusercontent.com/u/2283558?v=4)](https://github.com/yuks "yuks (3 commits)")[![ettiennelouw](https://avatars.githubusercontent.com/u/1754421?v=4)](https://github.com/ettiennelouw "ettiennelouw (3 commits)")[![foxted](https://avatars.githubusercontent.com/u/1385160?v=4)](https://github.com/foxted "foxted (3 commits)")[![hizbul25](https://avatars.githubusercontent.com/u/4462574?v=4)](https://github.com/hizbul25 "hizbul25 (3 commits)")[![montogeek](https://avatars.githubusercontent.com/u/1002461?v=4)](https://github.com/montogeek "montogeek (3 commits)")[![topcu](https://avatars.githubusercontent.com/u/1313469?v=4)](https://github.com/topcu "topcu (2 commits)")[![bourduard](https://avatars.githubusercontent.com/u/858898?v=4)](https://github.com/bourduard "bourduard (2 commits)")[![GregorDusan](https://avatars.githubusercontent.com/u/19596488?v=4)](https://github.com/GregorDusan "GregorDusan (2 commits)")[![Blum](https://avatars.githubusercontent.com/u/31083?v=4)](https://github.com/Blum "Blum (1 commits)")[![martijn-heil](https://avatars.githubusercontent.com/u/12084677?v=4)](https://github.com/martijn-heil "martijn-heil (1 commits)")[![michaeltintiuc](https://avatars.githubusercontent.com/u/1321256?v=4)](https://github.com/michaeltintiuc "michaeltintiuc (1 commits)")[![mkwsra](https://avatars.githubusercontent.com/u/1743919?v=4)](https://github.com/mkwsra "mkwsra (1 commits)")[![hasatbey](https://avatars.githubusercontent.com/u/10830079?v=4)](https://github.com/hasatbey "hasatbey (1 commits)")[![mvsvolkov](https://avatars.githubusercontent.com/u/8544605?v=4)](https://github.com/mvsvolkov "mvsvolkov (1 commits)")

---

Tags

carbonlaravellaravel5phplaraveldatetimetimedatecarbon

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dotzero-laravel-localized-carbon/health.svg)

```
[![Health](https://phpackages.com/badges/dotzero-laravel-localized-carbon/health.svg)](https://phpackages.com/packages/dotzero-laravel-localized-carbon)
```

###  Alternatives

[jenssegers/date

A date library to help you work with dates in different languages

1.8k11.2M79](/packages/jenssegers-date)[p3ym4n/jdate

Date converter from Jalali to Georgian and vice versa. It has Carbon instance inside and it's Laravel friendly.

101.8k2](/packages/p3ym4n-jdate)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
