ItSolutionStuff.com

Laravel Carbon Count Working Days Between Two Dates Example

By Hardik Savani • April 16, 2024
Laravel

Hey Friends,

Here, I will show you laravel carbon count working days between dates. We will use laravel carbon get difference between two dates in working days. I explained simply about calculate working days between two dates in laravel. We will use carbon difference between two dates in working days. Let's see below example how to get difference between two dates in working days in laravel carbon.

you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

In the following example you can see, we have two dates, the first $startDate variable and the second one $endDate variable. Carbon class diffInDays() function using you can get the difference between two dates in working days. I hope you will find your solution.

Example:

<?php

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);

}

}

Output

11

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Carbon Parse Date Format Example

Read Now →

How to Check Date is Today's Date or not in Laravel Carbon?

Read Now →

Laravel Carbon Time Format AM PM Example Code

Read Now →

How to use Carbon in Laravel Blade or Controller File?

Read Now →

Laravel Carbon createFromFormat() Example

Read Now →

Laravel Carbon Check Date is in Past Date Code

Read Now →

Laravel Carbon Get Last Day of Month Example

Read Now →

Laravel Carbon Get Year from Date Example

Read Now →

How to Compare Two Dates in Laravel Carbon?

Read Now →

Laravel Carbon Get Next Month Example

Read Now →

Laravel Carbon Get All Dates Between Two Dates Example

Read Now →

Laravel Carbon addHours() | Laravel Carbon Add Hours Example

Read Now →

Laravel Carbon Subtract Months from Date Example

Read Now →