English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of PHP date_interval_format() function

PHP Date & Time Function Manual

The date_interval_format() function is used to format time intervals.

Definition and usage

with other datesThis function is an alias of DateInterval::format. It accepts an interval and a format string as parameters and formats the given interval in the specified format.

Syntax

date_interval_format($interval, $format)

Parameter

Serial numberParameters and descriptions
1

interval (required)

This is the DateInterval object that you need to format.

2

format (required)

This is a string value that specifies the format.

Return value

This function returns a formatted interval

PHP version

This function was originally in the PHP version5.3中引入的,并且可以在所有更高版本中使用。

year

.introduced in and can be used in all higher versions.The following example demonstrates the usage of date_interval_format-

Online example
  It will not recalculate the rollover points in the date and time strings. Therefore, if you pass the date25()8Function usage9DP
  MP
  Y');
  );  
?>
Test and see‹/›

Output result

25 days

year

$format = "%d days";/$res = date_interval_format($interval, $format);with other datestime functions are different,/date_interval_format()

Online example
   It will not recalculate the rollover points in the date and time strings. Therefore, if you pass the date45Time values are out of their range, and they will be formatted as:
   $interval = new DateInterval('P',
   = date_interval_format($interval, $format);1 M');
   $format = "%m month";1$interval = date_diff($date 
  
   = date_interval_format($interval, $format);2 print($res3$res
   = date_interval_format(new DateInterval('PT',2$interval = date_diff($date
?>
Test and see‹/›

Output result

45 0H'), "%h hours");
3print("\n".$res

year

month0 hoursThe following example calculates the difference between a given date and the current date and uses-

Online example
   date_interval_format1 Function format result1989-09-25$date
   date_interval_format2 Function format result2012-09-09$date
   = date_create("1);2$interval = date_diff($date
   , $date
   );  
?>
Test and see‹/›

$res = date_interval_format($interval, '%Y year %d day');

22 print($res); 15 Output result:

year

Online example
print(date_interval_format(new DateInterval('P', "D'), "%d days")."\n");12Example
print(date_interval_format(new DateInterval('P', "D'), "%d days")."\n");7<?php
print(date_interval_format(new DateInterval('P', "D'), "%d days")."\n");12print(date_interval_format(new DateInterval('P', "M'), "%m months")."\n");
print(date_interval_format(new DateInterval('PT', "%h hours")."\n");9Y'), "%y years")."\n");
print(date_interval_format(new DateInterval('PT', "%h hours")."\n");45S'), "%s seconds")."\n");
?>
Test and see‹/›

Output result

22 years 7 days
12 days
7 months
12 years
9 hours
45 seconds