DatePeriod::__construct

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

DatePeriod::__constructCrée un nouvel objet DatePeriod

Description

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)

Crée un nouvel objet DatePeriod.

Liste de paramètres

start

La date de début de la période.

interval

L'intervalle entre les récurrences de la période.

recurrences

Le nombre de récurrences. Doit être plus grand que 0.

end

La date de fin de la période.

isostr

Une » spécification ISO 8601 de la répétition de l'intervalle. Les occurrences zéro (R0/) ne sont pas supportée.

options

Peut être configuré à DatePeriod::EXCLUDE_START_DATE pour exclure la date de début du jeu de dates de récursion dans la période.

Historique

Version Description
7.2.19, 7.3.6, 7.4.0 recurrences doit désormais être plus grand que 0.

Exemples

Exemple #1 Exemple avec 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';

// Toutes ces périodes sont équivalentes.
$period = new DatePeriod($start$interval$recurrences);
$period = new DatePeriod($start$interval$end);
$period = new DatePeriod($iso);

// En parcourant l'objet DatePeriod, toutes les
// dates de la récursion pour la période seront affichées.
foreach ($period as $date) {
    echo 
$date->format('Y-m-d')."\n";
}
?>

L'exemple ci-dessus va afficher :

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

Exemple #2 Exemple avec DatePeriod et 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);

// En parcourant l'objet DatePeriod,
// toutes les dates de récursion pour la période sont affichées.
// Notez que, dans ce cas, 2012-07-01 ne sera pas affiché.
foreach ($period as $date) {
    echo 
$date->format('Y-m-d')."\n";
}
?>

L'exemple ci-dessus va afficher :

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

Notes

Les nombres de répétitions non liés spécifiés dans la section 4.5 "Intervalle de temps récurrent" de la norme ISO 8601 ne sont pas supportés, c'est-à-dire ni "R/..." comme isostr ni null comme end ne fonctionnerait.

add a note add a note

User Contributed Notes 3 notes

up
34
simon dot kohlmeyer at mayflower dot de
13 years ago
I found two things useful to know that aren't covered here.

1. endDate is excluded:

<?php
$i
= new DateInterval('P1D');
$d1 = new Datetime();
$d2 = clone $d1; $d2->add($i);
foreach(new
DatePeriod($d1, $i, $d2) as $d) {
    echo
$d->format('Y-m-d H:i:s') . "\n";
}
?>

Will output:
2010-11-03 12:39:53

(Another one because I got it wrong at first)
2. For the first form, recurrences really means REcurrences, not occurences.

<?php
$i
= new DateInterval('P1D');
$d = new Datetime();
foreach(new
DatePeriod($d, $i, 1) as $d) {
    echo
$d->format('Y-m-d H:i:s') . "\n";
}
?>

Will output:
2010-11-03 12:41:05
2010-11-04 12:41:05
up
14
lars at hp-designs dot com
11 years ago
When you add the time 23:59:59 to the end DateTime object something like the following then the end date will be included in the period:

<?php
$date_start
= new DateTime('2012-03-12');
$date_end = new DateTime('2012-03-22 23:59:59');

$interval = '+2 days';
$date_interval = DateInterval::createFromDateString($interval);

$period = new DatePeriod($date_start, $date_interval, $date_end, DatePeriod::EXCLUDE_START_DATE);

foreach(
$period as $dt) {
echo
$dt->format('d/m');
}
?>

OUTPUT:
14/03
16/03
18/03
20/03
22/03
up
0
youssef dot benhssaien at gmail dot com
4 years ago
The parameter isostr should accept an ISO 8601 format, but doing this is not work :
<?php
      $dateStart
= date_create('first day of next month this year');

     
$isoStr = $dateStart ->format(DateTimeInterface::ISO8601);
     
$datePeriod = new DatePeriod("R10/{$isoStr}/P1M");
    
// Throw Exception : DatePeriod::__construct(): Unknown or bad format (R10/2020-02-01T22:52:26+0000Z/P1M)
    
     // While doing it by this way works
   
$datePeriod = new DatePeriod($dateStart , \DateInterval::createFromDateString("+1 month"), 10);
?>
To Top