PHP DatePeriod Class Methods

Author — Nitish Kumar

On This Page
  1. PHP DatePeriod::__construct() Method
    1. What does DatePeriod::__construct() do?
    2. PHP DatePeriod::__construct() Syntax
    3. PHP DatePeriod::__construct() Parameters
    4. PHP DatePeriod::__construct() Working Examples
      1. 1. DatePeriod example
      2. 2. DatePeriod example with DatePeriod::EXCLUDE_START_DATE
    5. Changelog for PHP DatePeriod::__construct() Method
    6. Additional Tips from Fellow Developers
  2. PHP DatePeriod::getDateInterval() Method
    1. What does DatePeriod::getDateInterval() do?
    2. PHP DatePeriod::getDateInterval() Syntax
    3. PHP DatePeriod::getDateInterval() Parameters
    4. PHP DatePeriod::getDateInterval() Return Value
    5. PHP DatePeriod::getDateInterval() Working Examples
      1. 1. DatePeriod::getDateInterval() example
  3. PHP DatePeriod::getEndDate() Method
    1. What does DatePeriod::getEndDate() do?
    2. PHP DatePeriod::getEndDate() Syntax
    3. PHP DatePeriod::getEndDate() Parameters
    4. PHP DatePeriod::getEndDate() Return Value
    5. PHP DatePeriod::getEndDate() Working Examples
      1. 1. DatePeriod::getEndDate() example
      2. 2. DatePeriod::getEndDate() without an end date
  4. PHP DatePeriod::getRecurrences() Method
    1. What does DatePeriod::getRecurrences() do?
    2. PHP DatePeriod::getRecurrences() Syntax
    3. PHP DatePeriod::getRecurrences() Parameters
    4. PHP DatePeriod::getRecurrences() Return Value
  5. PHP DatePeriod::getStartDate() Method
    1. What does DatePeriod::getStartDate() do?
    2. PHP DatePeriod::getStartDate() Syntax
    3. PHP DatePeriod::getStartDate() Parameters
    4. PHP DatePeriod::getStartDate() Return Value
    5. PHP DatePeriod::getStartDate() Working Examples
      1. 1. DatePeriod::getStartDate() example

PHP DatePeriod::__construct() Method

What does DatePeriod::__construct() do?

The PHP DatePeriod::__construct() method creates a new DatePeriod object.

PHP DatePeriod::__construct() Syntax

PHP DatePeriod::__construct() Parameters

  1. start — The start date of the period.

  2. interval — The interval between recurrences within the period.

  3. recurrences — The number of recurrences.

  4. end — The end date of the period.

  5. isostr — An ISO 8601 repeating interval specification.

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

PHP DatePeriod::__construct() Working Examples

1. DatePeriod example

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

Output of the above code:

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

2. DatePeriod example with 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";
}
?>

Output of the above code:

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

Changelog for PHP DatePeriod::__construct() Method

5.5.8 — end type changed to DateTimeInterface. Previously, DateTime.

5.5.0 — start type changed to DateTimeInterface. Previously, DateTime.

Additional Tips from Fellow Developers

Contributed By: simon dot kohlmeyer

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

Contributed By: lars

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

PHP DatePeriod::getDateInterval() Method

What does DatePeriod::getDateInterval() do?

The PHP DatePeriod::getDateInterval() method gets the interval .

PHP DatePeriod::getDateInterval() Syntax

 public DatePeriod::getDateInterval ( void ) : DateInterval

PHP DatePeriod::getDateInterval() Parameters

    This method does not accept any parameters.

    PHP DatePeriod::getDateInterval() Return Value

    The PHP DatePeriod::getDateInterval() method returns a DateInterval object

    PHP DatePeriod::getDateInterval() Working Examples

    1. DatePeriod::getDateInterval() example

    <?php
    $period = new DatePeriod('R7/2016-05-16T00:00:00Z/P1D');
    $interval = $period->getDateInterval();
    echo $interval->format('%d day');
    ?>

    Output of the above code:

    1 day

    PHP DatePeriod::getEndDate() Method

    What does DatePeriod::getEndDate() do?

    The PHP DatePeriod::getEndDate() method gets the end date .

    PHP DatePeriod::getEndDate() Syntax

     public DatePeriod::getEndDate ( void ) : DateTimeInterface

    PHP DatePeriod::getEndDate() Parameters

      This method does not accept any parameters.

      PHP DatePeriod::getEndDate() Return Value

      The PHP DatePeriod::getEndDate() method returns NULL if the DatePeriod does not have an end date. For example, when initialized with the recurrences parameter, or the isostr parameter without an end date.

      PHP DatePeriod::getEndDate() Working Examples

      1. DatePeriod::getEndDate() example

      <?php
      $period = new DatePeriod(
          new DateTime('2016-05-16T00:00:00Z'),
          new DateInterval('P1D'),
          new DateTime('2016-05-20T00:00:00Z')
      );
      $start = $period->getEndDate();
      echo $start->format(DateTime::ISO8601);
      ?>

      Output of the above code:

      2016-05-20T00:00:00+0000

      2. DatePeriod::getEndDate() without an end date

      <?php
      $period = new DatePeriod(
          new DateTime('2016-05-16T00:00:00Z'),
          new DateInterval('P1D'),
          7
      );
      var_dump($period->getEndDate());
      ?>

      Output of the above code:

      NULL

      PHP DatePeriod::getRecurrences() Method

      What does DatePeriod::getRecurrences() do?

      The PHP DatePeriod::getRecurrences() method gets the number of recurrences.

      PHP DatePeriod::getRecurrences() Syntax

       public DatePeriod::getRecurrences ( void ) : int

      PHP DatePeriod::getRecurrences() Parameters

        This method does not accept any parameters.

        PHP DatePeriod::getRecurrences() Return Value

        The PHP DatePeriod::getRecurrences() method returns the number of recurrences.

        PHP DatePeriod::getStartDate() Method

        What does DatePeriod::getStartDate() do?

        The PHP DatePeriod::getStartDate() method gets the start date .

        PHP DatePeriod::getStartDate() Syntax

         public DatePeriod::getStartDate ( void ) : DateTimeInterface

        PHP DatePeriod::getStartDate() Parameters

          This method does not accept any parameters.

          PHP DatePeriod::getStartDate() Return Value

          The PHP DatePeriod::getStartDate() method returns a DateTimeImmutable object when the DatePeriod is initialized with a DateTimeImmutable object as the start parameter.

          PHP DatePeriod::getStartDate() Working Examples

          1. DatePeriod::getStartDate() example

          <?php
          $period = new DatePeriod('R7/2016-05-16T00:00:00Z/P1D');
          $start = $period->getStartDate();
          echo $start->format(DateTime::ISO8601);
          ?>

          Output of the above code:

          2016-05-16T00:00:00+0000

          Rate this post —

          Very PoorPoorAverageGoodExcellent (No Ratings Yet)
          Loading...

          Tags: |

          Subscribe
          Notify of
          guest
          0 Comments
          Inline Feedbacks
          View all comments
          0%