DatePeriod::__construct

(PHP 5 >= 5.3.0, PHP 7)
Creates a new DatePeriod object
public DatePeriod::__construct ( DateTimeInterface $start , DateInterval $interval , int $recurrences [, int $options ] )
public DatePeriod::__construct ( DateTimeInterface $start , DateInterval $interval , DateTimeInterface $end [, int $options ] )
public DatePeriod::__construct ( string $isostr [, int $options ] )

Creates a new DatePeriod object.

Parameters:
start

The start date of the period.

interval

The interval between recurrences within the period.

recurrences

The number of recurrences.

end

The end date of the period.

isostr

An ISO 8601 repeating interval specification.

options

Can be set to DatePeriod::EXCLUDE_START_DATE to exclude the start date from the set of recurring dates within the period.

Changelog:
5.5.8

end DateTimeImmutable DateTime

5.5.0

start DateTimeImmutable DateTime

Examples:
DatePeriod example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$start new DateTime('2012-07-01');
$interval new DateInterval('P7D');
$end new DateTime('2012-07-31');
$recurrences = 4;
$iso 'R4/2012-07-01T00:00:00Z/P7D';
 
// All of these periods are equivalent.
$period new DatePeriod($start$interval$recurrences);
$period new DatePeriod($start$interval$end);
$period new DatePeriod($iso);
 
// By iterating over the DatePeriod object, all of the
// recurring dates within that period are printed.
foreach ($period as $date) {
    echo $date->format('Y-m-d')."\n";
}
?>

The above example will output:

2012-07-01
2012-07-08
2012-07-15
2012-07-22
2012-07-29
DatePeriod example with DatePeriod::EXCLUDE_START_DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$start new DateTime('2012-07-01');
$interval new DateInterval('P7D');
$end new DateTime('2012-07-31');
 
$period new DatePeriod($start$interval$end,
                         DatePeriod::EXCLUDE_START_DATE);
 
// By iterating over the DatePeriod object, all of the
// recurring dates within that period are printed.
// Note that, in this case, 2012-07-01 is not printed.
foreach ($period as $date) {
    echo $date->format('Y-m-d')."\n";
}
?>

The above example will output:

2012-07-08
2012-07-15
2012-07-22
2012-07-29
doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.