PHP date_interval_format() Function

Example

Calculate the interval between two dates and then format the time interval:

<?php
$date1=date_create("1984-01-28");
$date2=date_create("1980-10-15");
$diff=date_diff($date1,$date2);
// %a outputs the total days
echo $diff->format("Total days: %a.");
?>

Run Examples

Definition and Usage

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

The DateInterval::format() function is used to format time intervals.

Syntax

DateInterval::format(format);
Parameter Description
format

Required. Specify the format. The format parameter string can use the following characters:

  • % - Literal %
  • Y - Year with at least 2 digits, with leading zero (e.g., 03)
  • y - Year (e.g., 3)
  • M - Month with leading zero (e.g., 06)
  • m - Month (e.g., 6)
  • D - Day with leading zero (e.g., 09)
  • d - Day (e.g., 9)
  • a - Total days between two dates obtained by date_diff()
  • H - Hours with leading zero (e.g., 08, 23)
  • h - Hours (e.g., 8, 23)
  • I - Minutes with leading zero (e.g., 08, 23)
  • i - Minutes (e.g., 8, 23)
  • S - Seconds with leading zero (e.g., 08, 23)
  • s - Seconds (e.g., 8, 23)
  • R - A hyphen '-' when negative, a plus '+' when positive
  • r - A hyphen '-' when negative, empty when positive

Note:Each format string must start with a '%' symbol!

Technical Details

Return Value: Returns a formatted time interval.
PHP Version: 5.3+