How To Count Working Days In Laravel 9

Websolutionstuff | Jan-23-2023 | Categories : Laravel PHP

In this article, we will see how to count working days in laravel 9. Here, we will learn to calculate working days excluding weekends in laravel 7, laravel 8, and laravel 9 using carbon. You can calculate working days between two dates using the carbon function.

The carbon provides isWeekend() function to check weekends and also you can set custom weekend days using the setWeekendDays() function. You can get a number of working days between two dates in laravel 9 using carbon.

So, let's see the laravel 9 counts working days using carbon, how to calculate working days excluding weekends, how to exclude saturday and sunday between two dates, and calculate business days in laravel 8 and laravel 9.

Example:

In this example, we will see to calculate working days excluding Saturday and Sunday using carbon.

<?php

$dt = Carbon::create(2023, 1, 1);
$dt2 = Carbon::create(2023, 12, 31);

$daysForExtraCoding = $dt->diffInDaysFiltered(function(Carbon $date) {
   return $date->isWeekend();
}, $dt2);

echo $daysForExtraCoding;

Output:

104

 

Example:

In this example, you can set custom weekend days using the setWeekendDays() function.

<?php

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

$dt = Carbon::create(2023, 1, 1);
$dt2 = Carbon::create(2023, 12, 31);

$daysForExtraCoding = $dt->diffInDaysFiltered(function(Carbon $date) {
   return $date->isWeekend();
}, $dt2);

echo $daysForExtraCoding;

Output:

104

 

Example:

In this example, we will count the number of days in a month excluding weekends.

<?php

CarbonPeriod::macro('countWeekdays', static function () {
    return self::this()->filter('isWeekday')->count();
});
echo CarbonPeriod::create('2022-12-01', '2022-12-31')->countWeekdays();  

Output:

22

 


You might also like:

Recommended Post
Featured Post
How To Create Validation Rule In Laravel 10
How To Create Validation Rule...

In this article, we will see how to create a validation rule in laravel 10. Here, we will learn about the laravel 1...

Read More

May-22-2023

Laravel 8 Eloquent orWhereHas Condition
Laravel 8 Eloquent orWhereHas...

In this example we will see laravel 8 eloquent orWhereHas() condition. In previous example we will learn about ...

Read More

Oct-15-2021

Laravel 10 CRUD Operation Step By Step
Laravel 10 CRUD Operation Step...

In this article, we will see how to create a crud operation in laravel 10. Here, we will learn about laravel 10 crud ope...

Read More

Feb-24-2023

How To Get Last 15 Days Records In MySQL
How To Get Last 15 Days Record...

In this tutorial, we will see how to get the last 15 days records in MySQL PHP.  In PHP, you can use INTERVAL...

Read More

Feb-09-2022