PHP code example of ogzhncrt / date-range-helper

1. Go to this page and download the library: Download ogzhncrt/date-range-helper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ogzhncrt / date-range-helper example snippets


use Ogzhncrt\DateRangeHelper\DateRange;

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

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

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

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

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

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]

$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')]