Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

date_interval_format()

Example

Compute the interval between two dates, and then format the resulting 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.”);
?>

Definition and Usage

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

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

Syntax

DateInterval::format(format)

Parameter Values

Parameter

Description

format

Required. Specifies the format using the following characters in the format string:

  • % – Literal %
  • Y – Year, 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 number of days as a result of 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)
  • F – Microseconds with at least 6 digits (e.g., 004403, 235689)
  • f – Microseconds (e.g., 4403, 235689)
  • R – Sign “-” when negative, “+” when positive
  • r – Sign “-” when negative, empty when positive

Note: Each format character must be prefixed by a % sign.

Technical Details

Return Value:

Returns the interval formatted according to the specified format.

PHP Version:

5.3+

PHP Changelog:

In PHP 7.1, the F and f parameters were added.