Deprecated: Optional parameter $keys declared before required parameter $cms_id is implicitly treated as a required parameter in /home/www/dev/work/class/blog/CmsKey.php on line 75

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$title is deprecated in /home/www/dev/work/website/lvesu/class/controller/blog/php.php on line 28

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$outlink is deprecated in /home/www/dev/work/website/lvesu/template/blog/cms/php.manual.tpl on line 2

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$status is deprecated in /home/www/dev/work/website/lvesu/template/blog/index.head.php on line 2
PHP - Manual: DateInterval::format - 互联网笔记

略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: DateInterval::format

2025-05-18

DateInterval::format

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

DateInterval::formatFormats the interval

说明

public DateInterval::format(string $format): string

Formats the interval.

参数

format

The following characters are recognized in the format parameter string. Each format character must be prefixed by a percent sign (%).
format character Description Example values
% Literal % %
Y Years, numeric, at least 2 digits with leading 0 01, 03
y Years, numeric 1, 3
M Months, numeric, at least 2 digits with leading 0 01, 03, 12
m Months, numeric 1, 3, 12
D Days, numeric, at least 2 digits with leading 0 01, 03, 31
d Days, numeric 1, 3, 31
a Total number of days as a result of a DateTime::diff() or (unknown) otherwise 4, 18, 8123
H Hours, numeric, at least 2 digits with leading 0 01, 03, 23
h Hours, numeric 1, 3, 23
I Minutes, numeric, at least 2 digits with leading 0 01, 03, 59
i Minutes, numeric 1, 3, 59
S Seconds, numeric, at least 2 digits with leading 0 01, 03, 57
s Seconds, numeric 1, 3, 57
F Microseconds, numeric, at least 6 digits with leading 0 007701, 052738, 428291
f Microseconds, numeric 7701, 52738, 428291
R Sign "-" when negative, "+" when positive -, +
r Sign "-" when negative, empty when positive -,

返回值

Returns the formatted interval.

更新日志

版本 说明
7.2.12 The F and f format will now always be positive.
7.1.0 The F and f format characters were added.

示例

示例 #1 DateInterval example

<?php

$interval
= new DateInterval('P2Y4DT6H8M');
echo
$interval->format('%d days');

?>

以上示例会输出:

4 days

示例 #2 DateInterval and carry over points

<?php

$interval
= new DateInterval('P32D');
echo
$interval->format('%d days');

?>

以上示例会输出:

32 days

示例 #3 DateInterval and DateTime::diff() with the %a and %d modifiers

<?php

$january
= new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);

// %a will output the total number of days.
echo $interval->format('%a total days')."\n";

// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');

?>

以上示例会输出:

31 total days
1 month, 0 days

注释

注意:

The DateInterval::format() method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".

参见

添加备注

用户贡献的备注 1 note

up
1
online dot cloudmeetings dot marco at gmail dot com
7 months ago
Pls note these important abbreviations;

P: period
Y: years
M: months
D: days
T: time
H: hours
M: minutes
S: seconds

The above is from the ISO 8601 - an international standard that defines how to use, store, and transfer date, time, and duration information.

官方地址:https://www.php.net/manual/en/dateinterval.format.php

冷却塔厂家 广告
中文GPT4.0无需注册 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3