PHP date_interval_format() 函数

PHP 教程 · 2019-01-23 20:43:36

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

实例

<?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+

点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html

查看所有标签

Getting Real

Getting Real

Jason Fried、Heinemeier David Hansson、Matthew Linderman / 37signals / 2009-11-18 / USD 24.99

Getting Real details the business, design, programming, and marketing principles of 37signals. The book is packed with keep-it-simple insights, contrarian points of view, and unconventional approaches......一起来看看 《Getting Real》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具