Carbon: Constants for months just like we have for days?

Created on 1 Jul 2020  路  5Comments  路  Source: briannesbitt/Carbon


Hello,

This is not a bug report but more of a question/suggestion.
Apologies upfront if maybe this isn't the correct place to reach out.

I'm wondering if you have considered having constants for months, just like CarbonInterface has for days.
Personally, I've come across several situations where it would be useful to have human readable constants for the months, just because it makes the code slightly more readable and understandable, eg:

if ($month >= 2 && $month <= 7) {
    // apply some special business logic that should only happen between Feb and July.
}

vs

if ($month >= CarbonInterface::FEBRUARY && $month <= CarbonInterface::JULY) {
    // apply some special business logic that should only happen between Feb and July.
}

I know theoretically you could achieve the same with something like date_parse('July')['month'] or similar, but expressions can't (sadly) be defined as class constants and having those in CarbonInterface would be extremely helpful, imo.

Thanks!

enhancement good first issue

Most helpful comment

Hello,

The first thing to note is, it can actually be handled by any user-land interface:

interface Month
{
    const JANUARY = 1;
    const FEBRUARY = 2;
    const MARCH = 3;
    const APRIL = 4;
    const MAY = 5;
    const JUNE = 6;
    const JULY = 7;
    const AUGUST = 8;
    const SEPTEMBER = 9;
    const OCTOBER = 10;
    const NOVEMBER = 11;
    const DECEMBER = 12;
}

if ($month >= Month::FEBRUARY && $month <= Month::JULY) {
    // apply some special business logic that should only happen between Feb and July.
}

The days constants were here to help for week-end settings. I don't know if we should have those constants in CarbonInterface if we don't have a direct need for it in parameters of Carbon methods

All 5 comments

Hello,

The first thing to note is, it can actually be handled by any user-land interface:

interface Month
{
    const JANUARY = 1;
    const FEBRUARY = 2;
    const MARCH = 3;
    const APRIL = 4;
    const MAY = 5;
    const JUNE = 6;
    const JULY = 7;
    const AUGUST = 8;
    const SEPTEMBER = 9;
    const OCTOBER = 10;
    const NOVEMBER = 11;
    const DECEMBER = 12;
}

if ($month >= Month::FEBRUARY && $month <= Month::JULY) {
    // apply some special business logic that should only happen between Feb and July.
}

The days constants were here to help for week-end settings. I don't know if we should have those constants in CarbonInterface if we don't have a direct need for it in parameters of Carbon methods

@kylekatarnls thank you for your reply.

While you're right and I totally understand your point about not adding stuff the library doesn't actually use/need, I thought it would be worth bringing it up.
From a completeness and usefulness point of view, it could make some sense IMO, since PHP itself doesn't have constants for those in its core and writing a userland wrapper for every project you work on is tedious.

I'm OK to add them, this is open to pull requests.

Sure, I'm happy to give it a try, just wanted to first make sure it made sense from your side as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JonoB picture JonoB  路  5Comments

rohstar picture rohstar  路  4Comments

dboyadzhiev picture dboyadzhiev  路  4Comments

lorisleiva picture lorisleiva  路  3Comments

scs-ben picture scs-ben  路  3Comments