PHP 8.1.24 Released!

DatePeriod::__construct

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

DatePeriod::__constructCria um novo objeto DatePeriod

Descrição

public DatePeriod::__construct(
    DateTimeInterface $start,
    DateInterval $interval,
    int $recurrences,
    int $options = 0
)
public DatePeriod::__construct(
    DateTimeInterface $start,
    DateInterval $interval,
    DateTimeInterface $end,
    int $options = 0
)
public DatePeriod::__construct(string $isostr, int $options = 0)

Creates a new DatePeriod object.

DatePeriod objects can be used as an iterator to generate a number of DateTimeImmutable or DateTime object from a start date, a interval, and an end date or the number of recurrences.

The class of returned objects is equivalent to the DateTimeImmutable or DateTime ancestor class of the start object.

Parâmetros

start

A data inicial do período.

interval

A recorrência de intervalos entre os períodos.

recurrences

Número de recorrências.

end

A data final do período.

isostr

Uma especificação de repetição de intervalos ISO 8601.

options

Pode ser configurado com DatePeriod::EXCLUDE_START_DATE para excluir a data inicial do conjunto de recorrências dentro do período.

Changelog

Versão Descrição
5.5.8 O tipo de end foi modificado para DateTimeImmutable. Anteriormente era DateTime.
5.5.0 start foi modificado para DateTimeImmutable. Anteriormente era DateTime.

Exemplos

Exemplo #1 Exemplos de DatePeriod

<?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";
}
?>

O exemplo acima produzirá:

2012-07-01
2012-07-08
2012-07-15
2012-07-22
2012-07-29

Exemplo #2 Exemplo de DatePeriod com DatePeriod::EXCLUDE_START_DATE

<?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";
}
?>

O exemplo acima produzirá:

2012-07-08
2012-07-15
2012-07-22
2012-07-29

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top