zeeproject / date-range
Date range implementation
Installs: 1 259
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 5
Forks: 0
Open Issues: 3
Requires
- php: ~8.2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.16
- phpunit/phpunit: ^10.1
- psalm/plugin-phpunit: ^0.18.4
- scrutinizer/ocular: ^1.9
- vimeo/psalm: ^5.11
This package is auto-updated.
Last update: 2024-12-11 22:01:24 UTC
README
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.