PHP DateInterval class calculates the difference between two date/time objects. Sometimes, it requires finding the difference between two days, months, years, hours, minutes, and seconds for programming purposes. The object of this class stores the fixed amount of time that can be read by using the format() function using different format parameters. Different uses of the DateInterval class in PHP are shown in this tutorial.
Different Format Parameters of format() Function
The purposes of different format characters used in the format() function have been described in this part of the tutorial.
Format Character | Purpose |
%d | Prints the day of the date without leading zero
Example: 1, 2, 30, etc. |
%D | Prints the day of the date with a leading zero
Example: 2, 05, 30, etc. |
%m | Prints the month number of the date without a leading zero
Example: 1, 6, 12, etc. |
%M | Prints the month number of the date with a leading zero
Example: 01, 06, 12, etc. |
%y | Prints the 2-digit year
Example: 21, 22, 23, etc. |
%Y | Prints the 4-digit year
Example: 2021, 2022, 2023, etc. |
%h | Prints the hour without leading zero
Example: 1, 5, 11, etc. |
%H | Prints the hour with leading zero
Example: 01, 05, 11, etc. |
%i | Prints the minute without leading zero
Example: 1, 9, 59, etc. |
%I | Prints the minute with leading zero
Example: 01, 05, 25, etc. |
%s | Prints the second without leading zero
Example: 1, 9, 59, etc. |
%S | Prints the second with leading zero
Example: 01, 09, 59, etc. |
%f | Prints the microsecond without leading zero
Example: 7845, 562312, 235634, etc. |
%F | Prints the microsecond with 6 digits and leading zero
Example: 007845, 062312, 235634, etc. |
The uses of different methods of DateInterval class are discussed in the next part of this tutorial.
Example 1: Use of DateInterval Class for the Day, Month, and Year
Create a PHP file with the following script that shows the use of defining different interval values by using DateInterval class. The interval must start with the character, ‘P’. The character ‘Y’ is used for year interval, ‘M’ is used for month interval, and ‘D’ is used for day interval. In the script, the first DateInterval class will set 10 days intervals, the second DateInterval class will set 5 months and 10 days intervals, and the third DateInterval class will set 2 years, 5 months, and 10 days intervals.
//Set interval in days
$ival=new DateInterval('P10D');
//Print the interval value
echo "The interval is ", $ival->format('%D days.')."<br>";
//Set interval in months and days
$ival=new DateInterval('P5M10D');
//Print the interval value in months and days
echo "The interval is ", $ival->format('%M months and %D days.'), "<br>";
//Set interval in years, months and days
$ival=new DateInterval('P2Y5M10D');
//Print the interval value in years, months, and days with leading zeros
echo "The interval is ", $ival->format('%Y years, %M months and %D days.'), "<br>";
//Print the interval value in years, months, and days without leading zeros
echo "The interval is ", $ival->format('%y years, %m months and %d days.'), "<br>";
?>
The following output will appear after executing the previous script:
Example 2: Use of DateInterval Class With date_interval_format() Function
The date_interval_format() function prints the date interval value with the formatting string. This function takes the interval object in the first argument and the format string in the second argument. Create a PHP file with the following script where the use of the date_interval_format() function has been shown:
//Set the interval in years, months, and days
$ival = new DateInterval('P10Y6M15D');
//Set the format for days
$format = "%d";
//Print the interval value in days
echo "The interval is ".date_interval_format($ival, $format)." days.<br/>";
//Set the format for days and months
$format = "%m months and %days.";
//Print the interval value in months and days
echo "The interval is ".date_interval_format($ival, $format)."<br/>";
//Set the format for days
$format = "%y years, %m months, and %days.";
//Print the interval value in years, months, and days
echo "The interval is ".date_interval_format($ival, $format)."<br/>";
?>
The following output will appear after executing the previous script:
Example 3: Use of DateInterval Class for the Hour, Minute, and Second
The character ‘T’ is used after the character ‘P’ to set the interval in time. Create a PHP file with the following script where the time interval is set by using DateInterval class:
//Set interval in seconds
$ival = new DateInterval('PT30S');
//Set the formatting
$format = "%s seconds.";
//Print the interval value in seconds
echo "The interval is ". date_interval_format($ival, $format). "<br/>";
//Set interval in seconds
$ival = new DateInterval('PT15M30S');
//Set the formatting
$format = "%i minutes and %s seconds.";
//Print the interval value in minutes and seconds
echo "The interval is ". date_interval_format($ival, $format). "<br/>";
//Set interval in seconds
$ival = new DateInterval('PT5H15M30S');
//Set the formatting
$format = "%h hours, %i minutes and %s seconds.";
//Print the interval value in hours, minutes, and seconds
echo "The interval is ". date_interval_format($ival, $format). "<br/>";
?>
The following output will appear after executing the previous script:
Example 4: Use of DateInterval Class With createFromDateString() Function
The createFromDateString() function is used to set the date and time interval in different ways. Create a PHP file with the following script where the date and time interval has been set in various ways using the createFromDateString() function:
//Set interval in days
$ival = DateInterval::createFromDateString('20 days');
echo "The interval is ", $ival->format('%d days'), "<br/>";
//Set interval in weeks
$ival = DateInterval::createFromDateString('4 weeks');
echo "The interval is ", $ival->format('%d days'), "<br/>";
//Set interval in months
$ival = DateInterval::createFromDateString('3 months');
echo "The interval is ", $ival->format('%m months'), "<br/>";
//Set interval in years and days
$ival = DateInterval::createFromDateString('5 year + 15 days');
echo "The interval is ", $ival->format('%y years %d days'), "<br/>";
//Set interval in weeks, days, hours, and minutes by summation
$ival = DateInterval::createFromDateString('2 weeks + 15 days + 24 hours + 60 minutes');
echo "The interval is ", $ival->format('%d days, %h hours, and %i minutes'), "<br/>";
//Set interval in days by subtraction
$ival = DateInterval::createFromDateString('30 days - 15 days');
echo "The interval is ", $ival->format('%d days'), "<br/>";
?>
The following output will appear after executing the previous script:
Conclusion
The different ways of setting the date and time interval using string value at the time of creating the object of the DateInterval class or using createFromDateString() have been shown in this tutorial. The uses of the DateInterval class were discussed for the new PHP users to understand better.