Skip to content

suzuki86/DateRange

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 

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