PHP Tutorial - PHP date_interval_format() Function


The date_interval_format() function is an alias of DateInterval::format().

The DateInterval::format() function is used to format the interval.


PHP date_interval_format() Function has the following syntax.



ParameterIs RequiredDescription
formatRequired.Specifies the format.

The following characters can be used in the format parameter string:

%Literal %
YYear, at least 2 digits with leading zero (e.g 03)
yYear (e.g 3)
MMonth, with leading zero (e.g 06)
mMonth (e.g 6)
DDay, with leading zero (e.g 09)
dDay (e.g 9)
aTotal number of days as a result of date_diff()
HHours, with leading zero (e.g 08, 23)
hHours (e.g 8, 23)
IMinutes, with leading zero (e.g 08, 23)
iMinutes (e.g 8, 23)
SSeconds, with leading zero (e.g 08, 23)
sSeconds (e.g 8, 23)
RSign "-" when negative, "+" when positive
rSign "-" when negative, empty when positive


PHP date_interval_format() Function Each format character must be prefixed by a % sign!


PHP date_interval_format() Function returns the formatted interval.


Calculate the interval between two dates, then format the interval:


// %a outputs the total number of days
echo $diff->format("Total number of days: %a.");

The code above generates the following result.