Don’t forget to use Carbon constants

When working with dates and times, we use awesome Carbon class, right? And there’s a lot of small things in it which we might not even know. I will give you one example today.

True story: I had to write the IF-statement in the code which would say “if day of week is Saturday”. Of course, it’s easy:

if ($dt->dayOfWeek == ...

And then my brain went “But wait. What number is Saturday? Should be 6? Or 5, since the whole array is from 0 to 6? Oh come on…”

Then I went to Carbon docs and found out that you don’t have to remember such things – there are constants for that!

if ($dt->dayOfWeek == Carbon::SATURDAY) // ... do some Saturday magic

Yes, there are constants for all days of week:

var_dump(Carbon::SUNDAY);                          // int(0)
var_dump(Carbon::MONDAY);                          // int(1)
var_dump(Carbon::TUESDAY);                         // int(2)
var_dump(Carbon::WEDNESDAY);                       // int(3)
var_dump(Carbon::THURSDAY);                        // int(4)
var_dump(Carbon::FRIDAY);                          // int(5)
var_dump(Carbon::SATURDAY);                        // int(6)

But it doesn’t stop there. For those who are totally lost in time and don’t know how much years are in a century or hours in a day – there you go!

var_dump(Carbon::YEARS_PER_CENTURY);               // int(100)
var_dump(Carbon::YEARS_PER_DECADE);                // int(10)
var_dump(Carbon::MONTHS_PER_YEAR);                 // int(12)
var_dump(Carbon::WEEKS_PER_YEAR);                  // int(52)
var_dump(Carbon::DAYS_PER_WEEK);                   // int(7)
var_dump(Carbon::HOURS_PER_DAY);                   // int(24)
var_dump(Carbon::MINUTES_PER_HOUR);                // int(60)
var_dump(Carbon::SECONDS_PER_MINUTE);              // int(60)

Note to self: sometimes it’s quite useful to just read the manual of packages.

Like our articles?
Check out our Laravel online courses!


  1. > For those who are totally lost in time and don’t know how much years are in a century or hours in a day

    That isn’t what constants are for.

    Constants don’t exist because developers might not know how many years are in a century (obviously we all know the answer to that). Nor do they exist in case the value might change (clearly it won’t).

    These constants exist so that you can write them in your code instead of writing the number. Simple as that. They allow your code to be self-documenting: ie if your code divides a figure by 12 you might need a comment to explain why; if it divides by MONTHS_IN_YEAR instead, you won’t need that comment.


Please enter your comment!
Please enter your name here