- $y
- $m
- $d
- $h
- $i
- $s
- $invert
- $days
另有以下的方法 (method)
| 方法 | 說明 |
|---|---|
| __construct(string $interval_spec) | 建構子, $interval_spec 為時間字串。 |
| createFromDateString(string $time) | 建立時間間隔物件。 |
| format(string $format) | 依格式化字串輸出時間。 |
格式化字串依所用字元表示不同的意思,必須用百分比符號開頭 % ,如下列表
| 字元 | 說明 | 範例 |
|---|---|---|
| % | 表示 % | % |
| Y | 年 | 01, 03 |
| y | 年 | 1, 3 |
| M | 月 | 01, 03, 12 |
| m | 月 | 1, 3, 12 |
| D | 日 | 01, 03, 31 |
| d | 日 | 1, 3, 31 |
| a | 總天數或 unknown | 4, 18, 8123 |
| H | 時 | 01, 03, 23 |
| h | 時 | 1, 3, 23 |
| I | 分 | 01, 03, 59 |
| i | 分 | 1, 3, 59 |
| S | 秒 | 01, 03, 57 |
| s | 秒 | 1, 3, 57 |
| R | 正負號 | -, + |
| r | 負號,正號不顯示 | - |
舉例如下
<?php
$interval = new DateInterval('P2Y5M4DT6H8M');
echo $interval->format('%y years') . "\n";
echo $interval->format('%m months') . "\n";
echo $interval->format('%d days') . "\n";
echo $interval->format('%h hours') . "\n";
echo $interval->format('%i minutes') . "\n";
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:datetime03.php
功能:示範 PHP 程式
作者:張凱慶
時間:西元 2013 年 2 月 */
?>執行結果如下

| 中英文術語對照 | |
|---|---|
| 類別 | class |
| 物件 | object |
| 屬性 | property |
| 方法 | method |
您可以繼續參考
日期、時間相關的類別與函數
- DateTime
- DateTimeZone
- DateInterval
- checkdate()
- date_add()
- date_create_from_format()
- date_create()
- date_date_set()
- date_default_timezone_get()
- date_default_timezone_set()
- date_diff()
- date_format()
- date_get_last_errors()
- date_interval_create_from_date_string()
- date_interval_format()
- date_isodate_set()
- date_modify()
- date_offset_get()
- date_parse_from_format()
- date_parse()
- date_sub()
- date_sun_info()
- date_sunrise()
- date_sunset()
- date_time_set()
- date_timestamp_get()
- date_timestamp_set()
- date_timezone_get()
- date_timezone_set()
- date()
- getdate()
- gettimeofday()
- gmdate()
- gmmktime()
- gmstrftime()
- idate()
- localtime()
- microtime()
- mktime()
- strftime()
- strptime()
- strtotime()
- time()
- timezone_abbreviations_list()
- timezone_identifiers_list()
- timezone_location_get()
- timezone_name_from_abbr()
- timezone_name_get()
- timezone_offset_get()
- timezone_open()
- timezone_transitions_get()
- timezone_version_get()
相關目錄
回 PHP 快速導覽
回 PHP 教材
回首頁
參考資料
http://www.php.net/manual/en/class.dateinterval.php
http://www.php.net/manual/en/dateinterval.construct.php
http://www.php.net/manual/en/dateinterval.createfromdatestring.php
http://www.php.net/manual/en/dateinterval.format.php
沒有留言:
張貼留言