PHPackages                             ogzhncrt/date-range-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ogzhncrt/date-range-helper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ogzhncrt/date-range-helper
==========================

A small PHP utility to handle date ranges

v1.0.4(11mo ago)00MITPHPPHP ^8.1

Since Jul 24Pushed 11mo agoCompare

[ Source](https://github.com/ogzhncrt/date-range-helper)[ Packagist](https://packagist.org/packages/ogzhncrt/date-range-helper)[ RSS](/packages/ogzhncrt-date-range-helper/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (1)Versions (7)Used By (0)

📅 Date Range Helper
===================

[](#-date-range-helper)

A lightweight PHP library for working with date ranges in an elegant and immutable way.
Includes powerful utilities for comparing, shifting, merging, and analyzing ranges.

---

✅ Installation
--------------

[](#-installation)

```
composer require ogzhncrt/date-range-helper
```

---

🧠 Features
----------

[](#-features)

- Immutable `DateRange` class
- Range comparison (`contains`, `overlaps`)
- Range math (`shift`, `durationInDays`)
- Utility class `DateRangeUtils` for sorting &amp; merging ranges
- **Timezone support** with environment variable configuration
- **Business day calculations** with holiday and weekend support

---

🚀 Usage
-------

[](#-usage)

### ✨ Basic Creation

[](#-basic-creation)

```
use Ogzhncrt\DateRangeHelper\DateRange;

$range = DateRange::from('2024-01-01')->to('2024-01-10');
```

---

### 🔍 Check if a Date is Within the Range

[](#-check-if-a-date-is-within-the-range)

```
$range->contains(new DateTime('2024-01-05')); // true
$range->contains(new DateTime('2024-02-01')); // false
```

---

### 🔁 Shift Range Forward or Backward

[](#-shift-range-forward-or-backward)

```
$shifted = $range->shift(3);   // Jan 4 – Jan 13
$backward = $range->shift(-2); // Dec 30 – Jan 8
```

---

### 📏 Get Range Duration (in days)

[](#-get-range-duration-in-days)

```
$range->durationInDays(); // 10 (inclusive)
```

---

### 🔗 Check if Two Ranges Overlap

[](#-check-if-two-ranges-overlap)

```
$other = DateRange::from('2024-01-08')->to('2024-01-15');
$range->overlaps($other); // true
```

---

### 🌍 Timezone Support

[](#-timezone-support)

The library supports timezone configuration via environment variable `DATE_RANGE_HELPER_TIMEZONE`:

```
# Set timezone in your environment
export DATE_RANGE_HELPER_TIMEZONE="America/New_York"
```

```
// All date ranges will use the configured timezone
$range = DateRange::from('2024-01-01')->to('2024-01-10');
echo $range->getTimezone(); // "America/New_York"

// Convert to different timezone
$utcRange = $range->toTimezone('UTC');

// Get current configured timezone
echo DateRange::getConfiguredTimezone(); // "America/New_York"
```

---

### 💼 Business Day Calculations

[](#-business-day-calculations)

The library supports business day calculations with configurable weekends and holidays:

```
use Ogzhncrt\DateRangeHelper\Config\BusinessDayConfig;

// Configure weekends (Friday and Saturday)
BusinessDayConfig::setWeekendDays([5, 6]);

// Add holidays
BusinessDayConfig::addHoliday('2024-01-01');
BusinessDayConfig::addHolidays(['2024-12-25', '2024-12-26']);

// Load predefined holiday calendar
BusinessDayConfig::loadHolidayCalendar('US'); // US, EU, TR available

// Load holidays from API (recommended)
BusinessDayConfig::loadHolidaysFromAPI('US', 2024); // Any country, any year

// Business day operations (automatic holiday loading)
$range = DateRange::from('2024-01-01')->to('2024-01-07');
echo $range->businessDaysInRange(); // Automatically loads holidays for the range
echo $range->businessDaysInRange('US'); // Specify country for holidays

$shifted = $range->shiftBusinessDays(2, 'US'); // Shift by 2 business days
$expanded = $range->expandToBusinessDays('US'); // Expand to business days only

// Get business day periods
$businessRanges = $range->getBusinessDayRanges('US'); // Array of business day periods

// Multi-year ranges automatically load holidays for all years
$longRange = DateRange::from('2023-01-01')->to('2024-12-31');
echo $longRange->businessDaysInRange('US'); // Loads holidays for 2023 and 2024
```

---

### 🌍 Holiday API Integration

[](#-holiday-api-integration)

The library supports dynamic holiday data via external APIs:

```
use Ogzhncrt\DateRangeHelper\Config\HolidayAPI;

// Configure API (optional)
HolidayAPI::setPreferredAPI('nager'); // Free API, no key required
HolidayAPI::setApiKey('your-key'); // For Calendarific API

// Get holidays for any country
$holidays = HolidayAPI::getHolidays('US', 2024);
$holidays = HolidayAPI::getHolidays('FR', 2024); // France
$holidays = HolidayAPI::getHolidays('DE', 2024); // Germany

// Supported APIs:
// - Nager.Date API: 90+ countries, free, no API key
// - Calendarific API: 230+ countries, requires API key
```

---

### 🔄 Automatic Holiday Loading

[](#-automatic-holiday-loading)

The library automatically loads holidays for date ranges:

```
// Environment variable for default country
export DATE_RANGE_HELPER_COUNTRY="US"

// Automatic holiday loading for any range
$range = DateRange::from('2024-01-01')->to('2024-01-31');
echo $range->businessDaysInRange(); // Uses default country (US)
echo $range->businessDaysInRange('FR'); // Uses specific country

// Multi-year ranges automatically load holidays for all years
$longRange = DateRange::from('2022-01-01')->to('2024-12-31');
echo $longRange->businessDaysInRange('US'); // Loads holidays for 2022, 2023, 2024
```

---

🧰 DateRangeUtils
----------------

[](#-daterangeutils)

### 📚 Sort Ranges by Start Date

[](#-sort-ranges-by-start-date)

```
use Ogzhncrt\DateRangeHelper\DateRangeUtils;

$r1 = DateRange::from('2024-01-10')->to('2024-01-20');
$r2 = DateRange::from('2024-01-01')->to('2024-01-05');

$sorted = DateRangeUtils::sortRangesByStart([$r1, $r2]);
// Result: [$r2, $r1]
```

---

### 🧪 Merge Overlapping or Adjacent Ranges

[](#-merge-overlapping-or-adjacent-ranges)

```
$a = DateRange::from('2024-01-01')->to('2024-01-10');
$b = DateRange::from('2024-01-08')->to('2024-01-15');
$c = DateRange::from('2024-01-20')->to('2024-01-25');

$merged = DateRangeUtils::mergeRanges([$a, $b, $c]);
// Result: [DateRange('2024-01-01', '2024-01-15'), DateRange('2024-01-20', '2024-01-25')]
```

---

🧪 Testing
---------

[](#-testing)

```
./vendor/bin/phpunit
```

---

📄 License
---------

[](#-license)

MIT

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance52

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

5

Last Release

344d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7846509?v=4)[Oğuzhan Cerit](/maintainers/ogzhncrt)[@ogzhncrt](https://github.com/ogzhncrt)

---

Top Contributors

[![ogzhncrt](https://avatars.githubusercontent.com/u/7846509?v=4)](https://github.com/ogzhncrt "ogzhncrt (11 commits)")

---

Tags

phpdaterangedate-rangedate-range-helper

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ogzhncrt-date-range-helper/health.svg)

```
[![Health](https://phpackages.com/badges/ogzhncrt-date-range-helper/health.svg)](https://phpackages.com/packages/ogzhncrt-date-range-helper)
```

###  Alternatives

[kartik-v/yii2-date-range

An advanced Yii 2 date range picker input for based on bootstrap-daterangepicker plugin.

924.6M42](/packages/kartik-v-yii2-date-range)[dater/dater

Compact PHP library for working with date/time in different formats &amp; timezones.

14484.2k](/packages/dater-dater)[kartik-v/yii2-field-range

Easily manage Yii 2 ActiveField ranges (from/to) with Bootstrap 3 addons markup and more

252.2M26](/packages/kartik-v-yii2-field-range)[zjkal/time-helper

一个简单快捷的PHP日期时间助手类库。 a smart PHP datetime helper library.

21031.3k2](/packages/zjkal-time-helper)[maherelgamil/arabicdatetime

Easy and useful tool to generate arabic or hijri date with multi-language support for laravel

414.6k](/packages/maherelgamil-arabicdatetime)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
