kschu91/date-range

There is no license information available for the latest version (dev-master) of this package.

A small PHP library to extract date ranges out of a list of dates.

dev-master 2018-10-07 20:11 UTC

This package is auto-updated.

Last update: 2024-11-08 10:11:43 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

PHP Date Range

A small PHP library to extract date ranges out of a list of dates.

Installation

composer require "kschu91/date-range"

If you are not familiar with composer: composer basic usage

Requirements

  • PHP >= 7.1

Basic Usage

$datePeriods = (new DateRangeInterval(new \DateInterval('P1D'), $dates))->getDatePeriods();

Example

$dates = [
    new \DateTime('2018-09-02'),
    new \DateTime('2018-09-03'),
    new \DateTime('2018-09-04'),
    new \DateTime('2018-09-08'),
    new \DateTime('2018-10-02'),
    new \DateTime('2018-10-03'),
];

$range = new DateRangeInterval(new \DateInterval('P1D'), $dates);

$datePeriods = $range->getDatePeriods();

foreach ($datePeriods as $datePeriod) {
    echo $datePeriod->start->format('Y-m-d') . ' - ' . $datePeriod->end->format('Y-m-d') . PHP_EOL;
}

will output:

2018-09-02 - 2018-09-04
2018-09-08 - 2018-09-08
2018-10-02 - 2018-10-03