Count Weekend Days Between Two Dates Using Carbon In Laravel

Hello artisan,

In this carbon get day of week laravel tutorial, I will show you laravel carbon get difference between two dates in weekends days. This article will give you a simple example of calculating Saturday and Sunday days between two dates in laravel. This tutorial will help you to generate carbon difference between two dates on the weekends days.

In the following example, I will use two dates, the first $startDate variable and the second one $endDate variable. Then using the Carbon class diffInDaysFiltered() function using you can get the difference between two dates on weekends/holiday days. I hope you will find your solution.

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");
  
        $days = $startDate->diffInDaysFiltered(function (Carbon $date){
            return !$date->isWeekday();
        }, $endDate);
     
        dd($days);
    }
}

 

Read also: How to Get Difference Between Two Dates in Laravel Carbon

 

Hope it can help you.

 

#laravel #laravel-carbon