Carbon: holiday or festive days?

Created on 24 Apr 2014  路  10Comments  路  Source: briannesbitt/Carbon

it can be possible have some method like isWeekend but isHoliday establishing an array or by location, to know if date is on holiday?

documentation

Most helpful comment

And somes holydays (like Easter) are not same dates each year.
I think you better have to do your own extension :

MyCarbon extends Carbon {
    public function isHoliday() {
        return in_array([ $this->month, $this->day ], [
            [12, 25], // Christmas
            [01, 01], // New Year
            ...
        ]);
    }
}

All 10 comments

That would be incredibly complicated, do you know how many days are considered holidays all countries combined ?

I think, but if we would use, we can add a config array of days for example:
'Christmas' => [12,25], //to repeat festive days every year
'New Year' => [01,01],
'my festive' => [2014,05,01] //to only day timely festive

And somes holydays (like Easter) are not same dates each year.
I think you better have to do your own extension :

MyCarbon extends Carbon {
    public function isHoliday() {
        return in_array([ $this->month, $this->day ], [
            [12, 25], // Christmas
            [01, 01], // New Year
            ...
        ]);
    }
}

I think what Carbon needs is a macro functionality so you can add your own feature over it. If you need to define the holidays yourself this is pretty much what this would be.

Carbon::macro('isHoliday', function ($date) {
    return in_array($date->format('Y-m-d'), [holidays]); 
});

Carbon::parse('some-date')->isHoliday();

Something like that, that would allow people to add helpers for their edge cases, what do you think @briannesbitt ?

great! thanks!

There is a set of packages that is trying to solve just that.
https://github.com/Altruja/workday which makes use of https://github.com/Mayflower/libholiday

Good luck with that, incredibly complicated to do something like that! BTW, christmas in Germany is always the 24th of December, not the 25th.

I rather this be done in a sub class than adding macro functionality. If it then becomes flushed out and useful for others (and a common issue) then we can look at pulling it in.

I created a PR which adds Carbon extensions that check to see if a given date is a business day. Custom holiday and business schedules can be provided which allow you to properly determine what the date is for next or previous number of business days.

https://github.com/briannesbitt/Carbon/pull/706

Will appear in the documentation:
https://github.com/briannesbitt/Carbon/compare/gh-pages...kylekatarnls:gh-pages-1.26

Was this page helpful?
0 / 5 - 0 ratings