PHP date_interval_format() 函数


PHP date_interval_format() 函数

实例

计算两个日期间的间隔,然后格式化时间间隔:

<?php$date1=date_create("1984-01-28");$date2=date_create("1980-10-15");$diff=date_diff($date1,$date2);// %a 输出总天数echo $diff->format("总天数:%a.");?>

定义和用法

date_interval_format() 函数是 DateInterval::format() 的别名。

DateInterval::format() 函数用于格式化时间间隔。

语法

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 – 当负数时为符号 "-",当正数时为空

注释:每个格式字符串必须以 % 符号作为前缀!

技术细节

返回值: 返回已格式化的时间间隔。
PHP 版本: 5.3+
标签:, , ,