Home >>PHP Date Time Functions >PHP date_interval_format() Function

PHP date_interval_format() Function

PHP date_interval_format() Function

PHP date_interval_format() function is used to format the interval of the given dates. It is an alias of DateInterval::format().

Syntax:

DateInterval::format(format);

Parameter Values

Parameter Description
format This is a required parameter. This parameter defines the format.

Here is an example of date_interval_format() function in PHP:

<html>
<body>
<?php
$date1=date_create();
$date2=date_create("2020-02-15");
$diff=date_diff($date1,$date2);
echo $diff->format("Total number of days left: %a.");
?>
</body>
</html>

Output:
Total number of days left: 14.

Example2

<html>
<body>
<?php
$date1=date_create("01-01-2020");
$date2=date_create("31-12-2020");
$diff=date_diff($date1,$date2);
// %a outputs the total number of days
echo $diff->format("Total number of days is 2020: %a.");
echo "<br>";
// %R outputs + beacause of a positive interval
echo $diff->format("Total number of days in 2020: %R%a.");
echo "<br>";
// %d outputs the number of days that is not covered by the month
echo $diff->format("Month: %m, days: %d.");
?>
</body>
</html>
Output:
Total number of days is 2020: 365.
Total number of days in 2020: +365.
Month: 11, days: 30.

No Sidebar ads