DateRange
Immutable date range representation where left bound is inclusive, right bound is exclusive.
The following example range contains only one day:
$range = new DateRange(new DateTimeImmutable('2020-01-01'), new DateTimeImmutable('2020-01-02'));
Methods¶
__construct() ¶
|
|
Parameters
Name | Type | Default value | Description |
---|---|---|---|
$start | DateTimeInterface | - |
Left bound of the range (inclusive) |
$end | DateTimeInterface | - |
Right bound of the range (exclusive) |
Tags
__toString() ¶
Converts date range to string representation.
|
|
Example output: [2024-01-01T00:00:00+00:00 - 2024-01-02T00:00:00+00:00].
Return values
string
contains() ¶
Checks if given date is within the range.
|
|
Parameters
Name | Type | Default value | Description |
---|---|---|---|
$dateTime | DateTimeInterface | - | - |
Return values
bool
getEndDate() ¶
Returns right bound of the range (exclusive).
|
|
Return values
DateTimeInterface
getStartDate() ¶
Returns left bound of the range (inclusive).
|
|
Return values
DateTimeInterface
toDatePeriod() ¶
Converts date range (non-discrete value) to date period (discrete value) using given interval (optional, P1D used as default value).
|
|
Typical use case is to iterate over all the recurring the dates within range. For example:
$period = $range->toDatePeriod(new DateInterval('P1W'));
foreach ($period as $date) {
echo $date->format('Y-m-d')."\n";
}
Parameters
Name | Type | Default value | Description |
---|---|---|---|
$interval | DateInterval|null | null |
If not provided, then |
Return values
DatePeriod