PHP date_interval_format() 函數(shù)
PHP Date/Time 參考手冊
實(shí)例
計(jì)算兩個(gè)日期間的間隔,然后格式化時(shí)間間隔:
<?php
$date1=date_create("2013-01-01");
$date2=date_create("2013-02-10");
$diff=date_diff($date1,$date2);
// %a outputs the total number of days
echo $diff->format("Total number of days: %a.");
?>
運(yùn)行實(shí)例 ?
定義和用法
date_interval_format() 函數(shù)是 DateInterval::format() 的別名。
DateInterval::format() 函數(shù)用于格式化時(shí)間間隔。
語法
DateInterval::format(format);
參數(shù) | 描述 |
format | 必需。規(guī)定格式。format 參數(shù)字符串可以使用下列的字符: - % - Literal %
- Y - 年,至少 2 個(gè)數(shù)字,帶前導(dǎo)零(例如 03)
- y - 年(例如 3)
- M - 月,帶前導(dǎo)零(例如 06)
- m - 月(例如 6)
- D - 日,帶前導(dǎo)零(例如 09)
- d - 日(例如 9)
- a - 由 date_diff() 得出的兩個(gè)日期間隔的總天數(shù)
- H - 小時(shí),帶前導(dǎo)零(例如 08、23)
- h - 小時(shí)(例如 8、23)
- I - 分,帶前導(dǎo)零(例如 08、23)
- i - 分(例如 8、23)
- S - 秒,帶前導(dǎo)零(例如 08、23)
- s - 秒(例如 8、23)
- R - 當(dāng)負(fù)數(shù)時(shí)為符號 "-",當(dāng)正數(shù)時(shí)為符號 "+"
- r - 當(dāng)負(fù)數(shù)時(shí)為符號 "-",當(dāng)正數(shù)時(shí)為空
注意:每個(gè)格式字符串必須帶一個(gè) % 符作為前綴! |
技術(shù)細(xì)節(jié)
返回值: | 返回格式化的時(shí)間間隔。 |
PHP 版本: | 5.3+ |
PHP Date/Time 參考手冊
更多建議: