Deep dive into Carbon – Laracon Summer 2022 Recap

Discover the hidden powers of Carbon.

Ralph J. Smit Laravel Software Engineer

Exactly two weeks ago I had the honor of speaking at Laracon Online. It was a fantastic event and I discovered that I really loved the experience to speak and share my knowledge.

My talk was about Carbon. The aim was to show a few handy tricks that go beyond the basics and can turn out to be very useful when using Carbon in real-life. In this article I shortly summarized them for reference.

In general, I split up my talk in three points:

  1. Carbon
  2. CarbonInterval
  3. CarbonPeriod

If you prefer to watch instead of read, you can view the full Laracon talk here.

Tips about Carbon

The first thing I showed is the Carbon::hasFormat() function. This function can be used to determine whether a time string is in a certain format. This could be used, for example, in validation rules or in API validation.

Below an example of the Carbon::hasFormat() function:

$timestringA = ‘2022-09-14 09:41:00’;
$timestringB = ‘september 14th 2022 09:41’;
 
$hasValidFormat = Carbon::hasFormat($timestringA, ‘Y-m-d h:m:s’); // true
$hasValidFormat = Carbon::hasFormat($timestringB, ‘Y-m-d h:m:s’); // false

Explaining CarbonInterval

The next thing you can probably make good use of is the CarbonInterval class. An interval is a PHP object that contains information about a certain period of time. For example, if I were to say “I’ll meet you in 4 hours and 15 minutes”, we’re talking about an interval. It isn’t relevant when those 4 hrs and 15 minutes started or when they ended, we’re only talking about the fact that it is interval of 4 hours and 15 minutes.

Let’s see what we can use the CarbonInterval object for:

Carbon::now(); // September 14, 2022.
 
$carbonInterval = now()->diffAsCarbonInterval(
$firstOfAugust = now()->subMonth()->startOfMonth()
);
 
$daysDiff = $carbonInterval->d; // 13
$monthsDiff = $carbonInterval->m; // 1
 
$daysDiff = $carbonInterval->totalDays; // 41.403472222222
$monthsDiff = $carbonInterval->monts; // 1.4786954365079
 
$daysDiff = round($carbonInterval->totalDays); // 41

Another useful trick is sorting CarbonIntervals:

$intervalA = CarbonInterval::minutes(30);
$intervalB = CarbonInterval::minutes(10);
$intervalC = CarbonInterval::minutes(20);
 
$intervals = [$intervalA, $intervalB, $intervalC];
 
// Classic tuple-syntax:
usort($intervals, [CarbonInterval::class, ‘compareDateIntervals’]);
 
// Or, on PHP 8.1:
usort($intervals, CarbonInterval::compareDateIntervals());

The king of reducing ugly code: CarbonPeriod

The final thing I displayed, was using CarbonPeriod. The CarbonPeriod class is a very useful class that can be used to “split up” the period between two dates into smaller slices. For example, we could use CarbonPeriod to get the start of each week between January 1 2020 en December 31 2022. The cool thing is, we can even use the class to foreach over them. And taking into account every single edge case, like years with 53 weeks instead of 52 for example.

$carbonPeriod = CarbonPeriod::since(‘1-1-2020’)->until(’31-12-2022’)->interval(‘1 week’);
 
foreach ($carbonPeriod as $week) {
$startOfWeek = $week->startOfWeek();
 
// Do stuff.
}

You can easily make this as complex if you want. For example, if you want intervals for every second or third week. The best thing here is of course that it takes a boatload of responsibility from your shoulders, because you don’t have to account for every edge case anymore. Carbon does that automatically!

If you want to see how I refactor a part of ugly code to use CarbonPeriod, you can watch the final part of my talk where I demonstrate that.

My experience

I signed up for a lightning talk, so I learned that I should be to-the-point. A lightning talk is only fifteen minutes and my talk took in total about 19 minutes, so I went a few minutes over time. The next time I'll make sure to just skip one item. I was just too enthusiastic and I wanted to provide as much value as possible :)

Over all, it was an awesome experience and I'm really happy that I did it. Hopefully on to the next event!

Published by Ralph J. Smit on in Speaking .