PHP Tutorial - PHP date_interval_format() Function






Definition

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

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

Syntax

PHP date_interval_format() Function has the following syntax.

DateInterval::format(format);

Parameter

ParameterIs RequiredDescription
formatRequired.Specifies the format.

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

FormatMeaning
%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




Note

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

Return

PHP date_interval_format() Function returns the formatted interval.

Example

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


<?php
$date1=date_create("2013-01-01");
$date2=date_create("2013-02-10");
$diff=date_diff($date1,$date2);

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

The code above generates the following result.