Friday, September 5, 2025
HomeLanguagesPHP | DatePeriod getDateInterval() Function

PHP | DatePeriod getDateInterval() Function

The DatePeriod::getDateInterval() function is an inbuilt function in PHP which is used to return the date interval for the given date period.

Syntax:

DateInterval DatePeriod::getDateInterval( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns the DateInterval object of the given date period.

Below programs illustrate the DatePeriod::getDateInterval() function in PHP:

Program 1:




<?php
  
// Create a new DatePeriod object
$DP = new DatePeriod('R7/2019-09-25T12:30:00Z/P5D');
  
// Calling the DatePeriod::getDateInterval() function
$dateInterval = $DP->getDateInterval();
  
// Getting the date interval of the given DatePeriod
echo $dateInterval->format('%d days');
  
?>


Output:

5 days

Program 2:




<?php
  
// Declare the start date
$start_date = new DateTime('2019-09-01');
  
// Declare the DateInterval
$interval = new DateInterval('P5D');
  
// Declare the end date
$end_date = new DateTime('2019-09-30');
  
// Create a new DatePeriod object
$DP = new DatePeriod($start_date, $interval, $end_date);
  
// Calling the DatePeriod::getDateInterval() function
$dateInterval = $DP->getDateInterval();
  
// Getting the date interval of the given DatePeriod
echo $dateInterval->format('%d days');
  
?>


Output:

5 days

Reference: https://www.php.net/manual/en/dateperiod.getdateinterval.php

RELATED ARTICLES

Most Popular

Dominic
32267 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS