A Utility class for handling range of date written in PHP
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
tests
.travis.yml
README.md
composer.json
phpunit.xml

README.md

DateRange

A Utility class for handling range of date written in PHP.

Build Status

Installation

Installation with composer

Add some code like below to your composer.json.

{
  "repositories": [
    {
      "type": "git",
      "url": "https://github.com/suzuki86/DateRange"
    }
  ],
  "require": {
    "suzuki86/DateRange": "x.x.x"
  }
}

Usage

Namespace

Use DateRange\DateRange namespace.

use DateRange\DateRange;

DateRange::includes()

Check whether a certain date is included a certain date range.

$dateRange = new DateRange(
  strtotime('2015-10-01'),
  strtotime('2015-10-20')
);
$result = $dateRange->includes(strtotime('2015-10-10'));

var_dump($result); // bool(true)

DateRange::overlaps()

Check whether a certain date range overlaps with a certain date range.

$dateRange = new DateRange(
  strtotime('2015-10-01'),
  strtotime('2015-10-20')
);
$target = new DateRange(
  strtotime('2015-10-10'),
  strtotime('2015-10-25')
);
$result = $dateRange->overlaps($target);

var_dump($result); // bool(true)

DateRange::extract()

Extract included each date from a certain date range.

$dateRange = new DateRange(
  strtotime('2015-10-01'),
  strtotime('2015-10-05')
);
$result = $dateRange->extract();

echo date('Y-m-d', $result[0]); // '2015-10-01'
echo date('Y-m-d', $result[1]); // '2015-10-02'
echo date('Y-m-d', $result[2]); // '2015-10-03'
// ...

DateRange::getOverlap()

Get all overlapped dates.

$dateRange = new DateRange(
  strtotime('2015-10-01'),
  strtotime('2015-10-05')
);
$target = new DateRange(
  strtotime('2015-10-03'),
  strtotime('2015-10-07')
);
$resultDateRange = $dateRange->getOverlap($target);
echo strtotime('Y-m-d', $resultDateRange->startDate); // '2015-10-03'
echo strtotime('Y-m-d', $resultDateRange->endDate); // '2015-10-05'

DateRange::merge()

Merge a certain date range with another one.

$dateRange = new DateRange(
  strtotime('2015-10-01'),
  strtotime('2015-10-05')
);
$target = new DateRange(
  strtotime('2015-10-03'),
  strtotime('2015-10-07')
);
$resultDateRange = $dateRange->merge($target);
echo strtotime('Y-m-d', $resultDateRange->startDate); // '2015-10-01'
echo strtotime('Y-m-d', $resultDateRange->endDate); // '2015-10-07'