Count Working Days Between Two Dates Using Laravel Carbon

In this quick laravel carbon tips tutorial, I will show you laravel carbon count working days between dates. I will use laravel carbon get difference between two dates in working days with the source code. I will teach you how to calculate working days between two dates in laravel. I will use the carbon difference between the two dates in working days. Let's see below an example of how to get the difference between two dates in working days in laravel carbon.

In the following example, assume you have two dates, the first $startDate variable and the second one $endDate variable. We can use Carbon class diffInDays() function using you can get the difference between two dates in working days. Let's see the solution with example code:

Example code:

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $startDate = Carbon::parse("2022-11-01");
        $endDate = Carbon::parse("2022-11-21");
  
        $holidays = [
            Carbon::parse("2022-11-04"),
            Carbon::parse("2022-11-16"),
            Carbon::parse("2022-11-02"),
        ];
  
        $days = $startDate->diffInDaysFiltered(function (Carbon $date) use ($holidays) {
            return $date->isWeekday() && !in_array($date, $holidays);
        }, $endDate);
     
        dd($days);
    }
}

 

Read also: Count Weekend Days Between Two Dates Using Carbon in Laravel

 

Hope it can help you.

 

#laravel #laravel-carbon