PHP date_interval_format() 函数


PHP date_interval_format() 函数


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

实例

<?php  
$date1=date_create("2013-01-01");  
$date2=date_create("2013-02-10");  
$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+