Laravel 9 Count Weekends Days Between Two Dates Example

Reading Time: 2 minutes
982 Views

Inside this article we will see the concept i.e Laravel 9 Count Weekends Days Between Two Dates Example. Article contains the classified information about find total number of weekend days between two dates using Laravel carbon.

If you are looking for a solution i.e Laravel carbon package to get total days of weekends between two dates in application then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Read More: Laravel 9 Carbon Count Days Between Two Dates Example

What is Carbon in laravel?

Laravel framework uses a simple API extension to work with the date and time called Carbon. It’s a class.

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

How To Get Total Weekend Days Between Two Dates

Open project into terminal and run this command.

$ php artisan make:controller DemoController

It will create a DemoController.php file inside /app/Http/Controllers folder.

Read More: How To Generate PDF Using Laravel TCPDF Library Tutorial

Open file and write this code into it.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DemoController extends Controller
{
    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);
    }
}

Output

6

Above is the Total number of weekend days between the two given dates.

We hope this article helped you to learn Laravel 9 Count Weekends Days Between Two Dates Example in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.