Easy Carbon: Carbon makes working with dates very simple

Working with Carbon

Hi. Today I want to write about an essential php building block, I would even say that this library should be in every php-Project. What am I talking about? The Carbon library. This library works great with any of the latest php-frameworks, whether Symfony, Laravel, Yii2 or whatever your prefered framework is.

To install the library you need to either add it to composer.json:

{
 "require": {
 "nesbot / carbon": "~ 1.18"
 }
}

or in the console run the following command:

composer require nesbot/carbon

DateTime Plus

A Carbon object is derived from a standard php DateTime object, so we’re able to use all the standard DateTime methods plus some very convenient little extras from Carbon.

To create a Carbon object, start by writing the following code:

$now = Carbon::now();

The variable $now is used to get the current date and time of the server.

If on the other hand, you suddenly forget how old you are, but somehow still remember your birth date, you could use Carbon to help you resolve the problem simply, as follows:

$howOldAmI = Carbon::createFromDate(1985, 5, 21)->age;

A more usual situation which arises in projects is that we need to get a date which is the difference between two other dates, for example, today minus 2 weeks, 3 days and 12 minutes. Carbon makes this very simple:

$date = Carbon::now()->subWeeks(2)->subDays(3)->subMinutes(12)

That’s all!

Very often when working with a variety of third-party API’s, we get a string representation of the date which is in a non-standard form, eg (2015/3 31), Carbon can help us out in cases like this:

$date = Carbon::createFromFormat(“Y/n d”, “2015/3 31”)

Similarly, another common task is to calculate the difference between two dates in a variety of formats (in days, minutes, hours, etc.), this could be done as follows:

$dateCanada = Carbon::createFromDate(200, 1, 1, ‘America/Toronto’);
$dateAmerica = Carbon::createFromDate(200, 1, 1, ‘America/Vancouver’);
$diffInHours = $dateCanada->diffInHours($dateAmerica, false);
$diffInMinutes = $dateCanada->diffInMinutes($dateAmerica, false);
$diffInDays = $dateCanada->diffInDays($dateAmerica, false);

On another similar note, sometimes you need to simply calculate how much time has passed since a given date:

$strDiff = Carbon::now()->subDays(5)->diffForHumans(); // 5 days ago
$strDiff = Carbon::now()->diffForHumans(Carbon::now()->subYear()); // 1 year after

Carbon also supports localisation. You could set the output to German for example, using the following command:

Carbon::setLocale(‘de’);

Making things simple

Once this library appeared, it got a lot easier to work with dates. The examples above show how you can use one small and intuitively clear line to achieve something which before it was necessary to approach using several stages. I recommend looking at the documentation if you want to find out more, as this article covers only the most basic and frequently used Carbon techniques. Thank you for reading!