Back to KB
Difficulty
Intermediate
Read Time
3 min

How I Built Holiday-Aware Deadline Calculations in Laravel

By Codcompass TeamΒ·Β·3 min read

TL;DR: How to calculate business deadlines in Laravel that automatically skip weekends and holidays β€” using a database-driven holidays table and Carbon. Code examples included.

The Problem
When I was building VMMS β€” a voucher management system for government offices β€” I needed to calculate processing deadlines for each department.
Simple enough, right? Just add X days to the current date.
Wrong.

Government offices don't work on weekends. They don't work on holidays. A voucher submitted on Friday shouldn't have a Monday deadline if Monday is a holiday.
I needed a deadline calculator that was aware of all of this.

The Approach
Instead of hardcoding holidays, I stored them in a database table. This means adding new holidays requires no code changes β€” just a new database record.

Schema::create('holidays', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->date('date');
    $table->timestamps();
});

Enter fullscreen mode Exit fullscreen mode

Building the DateHelpers Service
I created a dedicated service class to handle all date calculations:

<?php

namespace App\Services;

use Carbon\Carbon;
use App\Models\Holiday;

class DateHelpers
{
    protected array $holidays;

    public function __construct()
    {
        $this->holidays = Holiday::pluck('date')
            ->map(fn($d) => Carbon::parse($d)->toDateString())
      

πŸŽ‰ Mid-Year Sale β€” Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register β€” Start Free Trial

7-day free trial Β· Cancel anytime Β· 30-day money-back