Skip to content

DateRange implementation, missing in PHP 📆

License

Notifications You must be signed in to change notification settings

slavcodev/date-range

Repository files navigation

Date Range

Tests status Code Coverage Quality Score GitHub issues Software License Latest Version on Packagist

Implementation of the Date Range missing in PHP.

Install

Using Composer

composer require zeeproject/date-range

Usage

DateRange - value object

Instantiating value object and accessing properties:

$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:

$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:

$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:

$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:

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:

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:

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

Testing

phpunit

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for more details.

About

DateRange implementation, missing in PHP 📆

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages