Latest web development tutorials

PHP date_interval_format () / DateInterval :: format () function

PHP Date / Time PHP Date / Time Reference Manual

The interval between two dates, and then format the interval:

Examples

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

//% A total output of between two dates Days
echo $ diff -> format ( "total number of days for the date interval:% a day.");
?>

Running instance »

Definition and Usage

date_interval_format () function is DateInterval :: format () alias.

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

grammar

DateInterval::format( format);

参数 描述
format 必需。规定格式。 format 参数字符串可以使用下列的字符:
  • % - Literal %
  • Y - 年,至少 2 个数字,带前导零(例如 03)
  • y - 年(例如 3)
  • M - 月,带前导零(例如 06)
  • m - 月(例如 6)
  • D - 日,带前导零(例如 09)
  • d - 日(例如 9)
  • a - 由 date_diff() 得出的两个日期间隔的总天数
  • H - 小时,带前导零(例如 08、23)
  • h - 小时(例如 8、23)
  • I - 分,带前导零(例如 08、23)
  • i - 分(例如 8、23)
  • S - 秒,带前导零(例如 08、23)
  • s - 秒(例如 8、23)
  • R - 当负数时为符号 "-",当正数时为符号 "+"
  • r - 当负数时为符号 "-",当正数时为空

注意:每个格式字符串必须带一个 % 符作为前缀!

technical details

return value: Returns formatted time interval.
PHP version: 5.3+


PHP Date / Time PHP Date / Time Reference Manual