2017 © Pedro Peláez
 

library date-range

Date range implementation

image

z-ee/date-range

Date range implementation

  • Friday, February 16, 2018
  • by slavcodev
  • Repository
  • 2 Watchers
  • 1 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Date Range

Tests status ![Code Coverage][ico-scrutinizer] Quality Score ![GitHub issues][ico-issues] Software License ![Latest Version on Packagist][ico-version], (*1)

Implementation of the Date Range missing in PHP., (*2)

Install

Using Composer, (*3)

composer require zeeproject/date-range

Usage

DateRange - value object

Instantiating value object and accessing properties:, (*4)

$range = new DateRange(new DateTime('-1 day'), new DateTime('+1 day'));
// Checking if range has start date
$range->hasStartDate();
// Accessing range start date
$range->getStartDate()->format('c');
// Checking if range has end date
$range->hasEndDate();
// Accessing range end date
$range->getEndDate()->format('c');
// Checking if range is finite
$range->isFinite();
// Checking if range already started
$range->isStarted();
// Checking if range already ended
$range->isEnded();
// Checking if range started on specific date
$range->isStartedOn(new DateTime());
// Checking if range ended on specific date
$range->isEndedOn(new DateTime());
// Accessing range duration in seconds
$range->getTimestampInterval();
// Accessing range interval
$range->getDateInterval()->format('%s');
// Printing
echo $range;
// Representing as JSON
json_encode($range);

Iterating over the range:, (*5)

$range = new DateRange(new DateTime('-1 day'), new DateTime('+1 day'));

foreach ($range->getDatePeriod(new DateInterval('P1D')) as $date) {
    echo $date->format('Y-m-d');
}

Splitting range into smaller ranges:, (*6)

$range = new DateRange(new DateTime('-1 day'), new DateTime('+1 day'));

foreach ($range->split(new DateInterval('P1D')) as $range) {
    echo $range;
}

Date range is immutable, any changes resulting to new object:, (*7)

$initial = new DateRange(new DateTime('-1 day'), new DateTime('+1 day'));
$actual = $initial->setStartDate(new DateTime('now'));
if ($initial === $actual) {
    throw new LogicException('Oh, ah');
}

DateRangeProvider - the date ranges builder.

Using builder to create new range with specific rules:, (*8)

class RangeForYear implements DateRangeProvider
{
    /**
     * @var int
     */
    private $year;

    /**
     * @param int $year
     */
    public function __construct(int $year)
    {
        $this->year = $year;
    }

    public function getDateRange(): DateRangeInterface
    {
        return new DateRange(
            new DateTimeImmutable(DateTimeImmutable::createFromFormat('c', "{$this->year}-01-01T00:00:00Z")),
            new DateTimeImmutable(DateTimeImmutable::createFromFormat('c', "{$this->year}-12-31T23:59:59Z"))
        );
    }
}

Your classes might depend on range provider instead of DateRange, useful when predefined ranges are more meaningful than range interface:, (*9)

class ReportCalculator
{
    public function calculate(DateRangeProvider $provider)
    {
        echo $provider->getDateRange();
    }
}

$calculator->calculate(new RangeForYear(2017));
$calculator->calculate(new RangeForQuarter(2017));
$calculator->calculate(new RangeForMonth(2017));

Even your class might require concrete range contract:, (*10)

class ReportCalculator
{
    public function calculate(FiniteDateRangeProvider $provider)
    {
        echo $provider->getDateRange();
    }
}

Testing

phpunit

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for more details., (*11)

The Versions

16/02 2018

dev-master

9999999-dev

Date range implementation

  Sources   Download

BSD-2-Clause

The Requires

  • php ^7.1
  • ext-json *

 

The Development Requires

by Veaceslav Medvedev

date range

16/02 2018

0.4.0

0.4.0.0

Date range implementation

  Sources   Download

BSD-2-Clause

The Requires

  • php ^7.1
  • ext-json *

 

The Development Requires

by Veaceslav Medvedev

date range

16/02 2018

dev-range-timestamp-duration

dev-range-timestamp-duration

Date range implementation

  Sources   Download

BSD-2-Clause

The Requires

  • php ^7.1
  • ext-json *

 

The Development Requires

by Veaceslav Medvedev

date range

18/11 2017

0.3.0

0.3.0.0

Date range implementation

  Sources   Download

BSD-2-Clause

The Requires

  • php ^7.1
  • ext-json *

 

The Development Requires

by Veaceslav Medvedev

date range

17/11 2017

0.1.1

0.1.1.0

Date range implementation

  Sources   Download

BSD-2-Clause

The Requires

  • php ^7.1
  • ext-json *

 

The Development Requires

by Veaceslav Medvedev

date range

16/11 2017

0.0.1

0.0.1.0

Date range implementation

  Sources   Download

BSD-2-Clause

The Requires

  • php ^7.1
  • ext-json *

 

The Development Requires

by Veaceslav Medvedev

date range