Copied!

DateRange

DateRange.php : 26

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

public__construct()

DateRange.php : 40
public __construct(DateTimeInterface $start, DateTimeInterface $end)

Parameters

Name Type Default value Description
$start DateTimeInterface -

Left bound of the range (inclusive)

$end DateTimeInterface -

Right bound of the range (exclusive)

Tags
Throws
RangeException

if $start > $end

public__toString()

DateRange.php : 111

Converts date range to string representation.

public __toString() : string

Example output: [2024-01-01T00:00:00+00:00 - 2024-01-02T00:00:00+00:00].

Return values

string

publiccontains()

DateRange.php : 77

Checks if given date is within the range.

public contains(DateTimeInterface $dateTime) : bool

Parameters

Name Type Default value Description
$dateTime DateTimeInterface - -

Return values

bool

publicgetEndDate()

DateRange.php : 69

Returns right bound of the range (exclusive).

public getEndDate() : DateTimeInterface

Return values

DateTimeInterface

publicgetStartDate()

DateRange.php : 61

Returns left bound of the range (inclusive).

public getStartDate() : DateTimeInterface

Return values

DateTimeInterface

publictoDatePeriod()

DateRange.php : 97

Converts date range (non-discrete value) to date period (discrete value) using given interval (optional, P1D used as default value).

public toDatePeriod([DateInterval|null $interval = null ]) : DatePeriod

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 DateInterval('P1D') is default value.

Return values

DatePeriod