Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change what days Carbon considers the weekend? [closed]

I want to change weekendDays to only include Sunday in my Carbon Instance. How can I do that?

like image 593
dhemit.lennon Avatar asked Nov 18 '17 14:11

dhemit.lennon


People also ask

How to set date in Carbon?

Carbon also allows us to generate dates and times based on a set of parameters. For example, to create a new Carbon instance for a specific date use the Carbon::createFromDate() method, passing in the year, month, day, and timezone, as in the following example.

How do you convert a string to a carbon date?

$date = Carbon\Carbon::parse($rawDate); well thats it. You'll now have a Carbon instance & you can format the date as you like using Carbon helper functions.

What is the use of carbon in laravel?

Most web applications require to work with date and time. The web application developed by the Laravel framework uses a simple API extension to work with the date and time called Carbon. This PHP package can handle the time and timezone more easily in the Laravel project.


1 Answers

Run:

Carbon::setWeekendDays([Carbon::SUNDAY]);

In your App\Providers\AppServiceProvider in the boot function.

Edit:

setWeekendDays is deprecated. Use macro instead.

Carbon::macro('isDayOff', function ($date) {
   return $date->isSunday();
});

$isDayOff = $carbon_inst->isDayOff(): bool;
like image 58
Laraleg Avatar answered Sep 29 '22 20:09

Laraleg