How To Count Days Excluding Weekends And Holidays In Laravel 9

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

In this article, we will see how to count days excluding weekends and holidays in laravel 9. Here, we will learn to skip weekends and holidays in laravel 7, laravel 8 and laravel 9 using carbon. We will use laravel carbon to get the difference between the two dates and calculate weekdays and holidays.

Carbon provides diffInDaysFiltered()diffInHoursFiltered() and diffFiltered(), to help you filter the difference by days, hours or a custom interval. For example, to count the weekend days between two dates.

So, let's see PHP count days excluding weekends and holidays, laravel 9 skip weekends and holidays, how to exclude weekends and holidays in laravel 8 and laravel 9, and laravel carbon count working days between two dates.

Example:

In this example, we will use carbon function.

<?php

$dt = Carbon::create(2023, 1, 1);
$dt2 = Carbon::create(2023, 12, 31);
$holidays = [
    Carbon::parse("2023-01-26"),
    Carbon::parse("2023-08-15"),    
];

$daysForExtraCoding = $dt->diffInDaysFiltered(function (Carbon $date) use ($holidays) {
   
   return $date->isWeekday() && !in_array($date, $holidays);
}, $dt2);

echo $daysForExtraCoding;

Output:

258

 

Example:

In this example, you can add a custom holiday list and get the number of days excluding weekdays using the carbon function.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class UserController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $startDate = Carbon::parse("2023-01-01");
        $endDate = Carbon::parse("2023-12-31");
  
        $holidays = [
            Carbon::parse("2023-01-26"),
            Carbon::parse("2023-08-15")
        ];
  
        $days = $startDate->diffInDaysFiltered(function (Carbon $date) use ($holidays) {
            return $date->isWeekday() && !in_array($date, $holidays);
        }, $endDate);
     
        dd($days);
    }
}

Output:

258

 


You might also like:

Recommended Post
Featured Post
How To Get Element By Data Attribute In jQuery
How To Get Element By Data Att...

In this article, we'll learn how to locate elements using data-attribute values. We can do this using jQuery and CSS...

Read More

Jul-11-2022

Laravel 9 Generate PDF File Using DomPDF
Laravel 9 Generate PDF File Us...

In this tutorial, we will see laravel 9 generate pdf file using dompdf. For generating pdf files we will use the la...

Read More

Feb-25-2022

How To Create Pie Chart In Laravel 9 Using Highcharts
How To Create Pie Chart In Lar...

In this article, we will see how to create a pie chart in laravel 9 using highcharts. A pie chart is a circular statisti...

Read More

Oct-05-2022

Laravel 8 One To Many Relationship Example
Laravel 8 One To Many Relation...

In this example we will see laravel 8 one to many relationship example. Also you can use one to many relationship in lar...

Read More

Nov-03-2021