Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GTHell's avatar

How to use CarbonPeriod?

There is no Carbon in composer.json. I can't update it and I need to use CarbonPeriod.

0 likes
6 replies
tykus's avatar

Carbon is a Laravel Framework dependency, so it should already be available in the project albeit not explicitly included in the top-level composer.json. If you are able to use Carbon\Carbon, then you will be able to use Carbon\CarbonPeriod

GTHell's avatar

If I able to use the CarbonPeriod this question wouldn't be exist. I can't find Carbon anywhere in composer.json

Cronix's avatar

Which composer.json file are you looking at? There are probably hundreds that you are actually using. Your app? The frameworks (Illuminate)? All of the other composer.jsons in each package requiring other packages? Each package has their own, which can require other packages. That's how both composer and node work. Laravel is made up of many packages, from all sorts of vendors.

In your apps composer.json, you require the laravel framework:

"laravel/framework": "5.7.*",

That gets installed in /vendor/laravel/framework. Now go in that dir and look at the packages laravel requires in its composer.json, which means laravel installs them when you require laravel/framework.

You will see it is requiring (among many, many other things)

"nesbot/carbon": "^1.26.3",

That gets installed in /vendor/nesbot/carbon. Go look in the src dir in that dir. You will find the Carbon class, CarbonInterval class and CarbonPeriod class.

So, if you're using laravel, you have carbon (and CarbonInterval/CarbonPeriod) installed and accessible. Otherwise you couldn't use Carbon\Carbon anywhere, and laravel couldn't use it, which it does all over the place. So you just need to use Carbon\CarbonPeriod as @tykus has been saying

You also need to new up a CarbonPeriod instance like the docs show. https://carbon.nesbot.com/docs/#api-period

1 like
GTHell's avatar

Like I said, Carbon\CarbonPeriod doesn't exist. However I manage to get it work by adding "nesbot/carbon": "1.29.0 as 1.25.0" to composer.json

Cronix's avatar

Are you not using laravel then if it isn't available by default?

There is no Carbon in composer.json. I can't update it

Of course you can get it to work by adding it to composer.json, but you originally stated that you couldn't do that.

GTHell's avatar

Carbon\Carbon and Carbon\CarbonInterval available by default but not Carbon\CarbonPeriod I don't know why. I didn't say that I can't do that. Adding new Carbon package will conflict with existing one unless alias it to 1.25.0

1 like

Please or to participate in this conversation.