DateInterval::format
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
DateInterval::format — 간격 형식 지정
설명
public DateInterval::format(string $format
): string
Formats the interval.
매개변수
format
-
다음 문자는
format
매개변수 문자열에서 인식됩니다. 각 형식 문자는 퍼센트 기호(%
)로 시작해야 합니다.format
character설명 예시 값 %
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)
otherwise4
,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-
,
반환 값
형식이 지정된 간격을 반환합니다.
변경 로그
버전 | 설명 |
---|---|
7.1.0 | F 및 f 형식 문자가 추가되었습니다. |
Examples
예제 #1 DateInterval 예제
<?php
$interval = new DateInterval('P2Y4DT6H8M');
echo $interval->format('%d days');
?>
위의 예는 다음을 출력합니다.
4 days
예제 #2 DateInterval 및 이월 포인트
<?php
$interval = new DateInterval('P32D');
echo $interval->format('%d days');
?>
위의 예는 다음을 출력합니다.
32 days
예제 #3 %a 및 %d 수정자가 있는 DateInterval 및 DateTime::diff()
<?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
메모
메모: DateInterval::format() 메서드는 시간 문자열이나 날짜 세그먼트의 이월 지점을 다시 계산하지 않습니다. 이는 "1 month and 4 days"
에서 "1 month and 1 day"
까지로 해석될 수 있는 "32 days"
과 같은 값을 오버플로할 수 없기 때문에 예상됩니다.
기타
- DateTime::diff() - 두 DateTime 객체 간의 차이를 반환합니다.