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.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 58 courses (1054 lessons, total 46 h 42 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials