Skip to content

A Utility class for handling range of date written in PHP

Notifications You must be signed in to change notification settings

suzuki86/DateRange

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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'

About

A Utility class for handling range of date written in PHP

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages