Carbon: Return morning, afternoon, evening or night

Created on 11 Jan 2018  路  9Comments  路  Source: briannesbitt/Carbon

I thought it would be a nice idea if Carbon could return morning, afternoon, evening or night, of a date

enhancement

Most helpful comment

IMO, this should come in an extended class, or the macro/extension system if we do it.

Then, I would not change a public member, it should rather be a getter method.

This how you can deal with that with an extended class (or the macro() if you use laravel):

// extend way
class MyCarbon extends Carbon
{
    public function greet()
    {
        $hour = $this->format('H');
        if ($hour < 12) {
            return 'Good morning';
        }
        if ($hour < 17) {
            return 'Good afternoon';
        }
        return 'Good evening';
    }
}

// macro way (with Laravel)
Carbon::macro('greet', function () {
    $hour = $this->format('H');
    if ($hour < 12) {
        return 'Good morning';
    }
    if ($hour < 17) {
        return 'Good afternoon';
    }
    return 'Good evening';
});

All 9 comments

Can u show us some examples of what you mean?

Considering 1 = Morning, 2 = Afternoon and 3 = Evening, an example could look something like this:

$dt = Carbon::parse('2017-2-2 00:00:00');
var_dump($dt->greeting); // returns 1, for morning, or simply returns "Good Morning"

Or

$dt = Carbon::parse('2017-2-2 12:02:00');
var_dump($dt->greeting); // returns 2, for afternoon, or simply returns "Good Afternoon"

Or

$dt = Carbon::parse('2017-2-2 17:00:00'); // or starting after 18:00 (6pm)
var_dump($dt->greeting); // returns 3, for evening, or simply returns "Good Evening"

Does Carbon need such??

Interesting. Let's see what others have to say about this.

IMO, this should come in an extended class, or the macro/extension system if we do it.

Then, I would not change a public member, it should rather be a getter method.

This how you can deal with that with an extended class (or the macro() if you use laravel):

// extend way
class MyCarbon extends Carbon
{
    public function greet()
    {
        $hour = $this->format('H');
        if ($hour < 12) {
            return 'Good morning';
        }
        if ($hour < 17) {
            return 'Good afternoon';
        }
        return 'Good evening';
    }
}

// macro way (with Laravel)
Carbon::macro('greet', function () {
    $hour = $this->format('H');
    if ($hour < 12) {
        return 'Good morning';
    }
    if ($hour < 17) {
        return 'Good afternoon';
    }
    return 'Good evening';
});

Interesting but I'm not sure if culturally possible. I travel a lot and know from other countries that they use evening already when for me it is still afternoon. Is there any "universal" definition when evening / night starts?

This is all very subjective. Especially around DST changes, some people will look at the clock and start saying "good evening" and then look outside and change to "good afternoon" (or the other way around).

This may be a language thing, but I wouldn't say "good morning" to anyone I was meeting before dawn (as the stated example shows that anything from midnight to noon would be "good morning"). If this were to be implemented, you'd need to be able to define how many intervals there are, and be able to easily modify those intervals to adjust for changes to length of days.

I think it would have to be based on the amount of daytime sunlight for that day. In the summer we start saying good evening later in the day compared to the darker winter months. Not to mention the translations ... oh boy ! Seems better suited for an extended class.

@kylekatarnls any clue why I'm getting an "Undefined method Carbon::macro()" exception?
Using Laravel 5.5, I checked the source and Carbon does in fact use Macroable; so I'm really not sure what's that about...

Hi, first we're implementing macro natively (no longer need of Laravel Macroable) for the next version, do you use the "dev-master" version? else can you tell me your current Carbon version?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JonoB picture JonoB  路  5Comments

dboyadzhiev picture dboyadzhiev  路  4Comments

lorisleiva picture lorisleiva  路  3Comments

MaxGiting picture MaxGiting  路  4Comments

hackel picture hackel  路  5Comments