# 日期輔助函數
日期輔助函數文件包含了一些幫助你處理日期的函數。
[TOC=2,3]
## [加載輔助函數](http://codeigniter.org.cn/user_guide/helpers/date_helper.html#id6)
該輔助函數通過下面的代碼加載:
~~~
$this->load->helper('date');
~~~
## [可用函數](http://codeigniter.org.cn/user_guide/helpers/date_helper.html#id7)
該輔助函數有下列可用函數:
now([$timezone = NULL])
參數:
* **$timezone**?(string) -- Timezone
返回: UNIX timestamp
返回類型: int
根據服務器的本地時間,以及一個 PHP 支持的時區參數或配置文件中的 "基準時間" 參數返回當前時間的 UNIX 時間戳, 如果你不打算設置 "基準時間" (如果你的站點允許用戶設置他們自己的時區,你通常需要設置這個), 該函數就和 PHP 的?time()?函數沒什么區別。
~~~
echo now('Australia/Victoria');
~~~
如果沒有指定時區,該函數將使用?**time_reference**?參數調用?time()?函數。
mdate([$datestr = ''[,?$time = '']])
參數:
* **$datestr**?(string) -- Date string
* **$time**?(int) -- UNIX timestamp
返回: MySQL-formatted date
返回類型: string
該函數和 PHP 的?[date()](http://php.net/manual/en/function.date.php)?函數一樣, 但是它支持 MySQL 風格的日期格式,在代碼之前使用百分號,例如:%Y %m %d
使用這個函數的好處是你不用關心去轉義那些不是日期代碼的字符,如果使用?date()?函數時,你就要這么做。
例如:
~~~
$datestring = 'Year: %Y Month: %m Day: %d - %h:%i %a';
$time = time();
echo mdate($datestring, $time);
~~~
如果第二個參數沒有提供一個時間,那么默認會使用當前時間。
standard_date([$fmt = 'DATE_RFC822'[,?$time = NULL]])
參數:
* **$fmt**?(string) -- Date format
* **$time**?(int) -- UNIX timestamp
返回: Formatted date or FALSE on invalid format
返回類型: string
生成標準格式的時間字符串。
例如:
~~~
$format = 'DATE_RFC822';
$time = time();
echo standard_date($format, $time);
~~~
注解
該函數已經廢棄,請使用原生的?date()?函數和?[時間格式化常量](http://php.net/manual/en/class.datetime.php#datetime.constants.types)?替代:
~~~
echo date(DATE_RFC822, time());
~~~
**支持的格式:**
| Constant | Description | Example |
| --- | --- | --- |
| DATE_ATOM | Atom | 2005-08-15T16:13:03+0000 |
| DATE_COOKIE | HTTP Cookies | Sun, 14 Aug 2005 16:13:03 UTC |
| DATE_ISO8601 | ISO-8601 | 2005-08-14T16:13:03+00:00 |
| DATE_RFC822 | RFC 822 | Sun, 14 Aug 05 16:13:03 UTC |
| DATE_RFC850 | RFC 850 | Sunday, 14-Aug-05 16:13:03 UTC |
| DATE_RFC1036 | RFC 1036 | Sunday, 14-Aug-05 16:13:03 UTC |
| DATE_RFC1123 | RFC 1123 | Sun, 14 Aug 2005 16:13:03 UTC |
| DATE_RFC2822 | RFC 2822 | Sun, 14 Aug 2005 16:13:03 +0000 |
| DATE_RSS | RSS | Sun, 14 Aug 2005 16:13:03 UTC |
| DATE_W3C | W3C | 2005-08-14T16:13:03+0000 |
local_to_gmt([$time = ''])
參數:
* **$time**?(int) -- UNIX timestamp
返回: UNIX timestamp
返回類型: int
將時間轉換為 GMT 時間。
例如:
~~~
$gmt = local_to_gmt(time());
~~~
gmt_to_local([$time = ''[,?$timezone = 'UTC'[,?$dst = FALSE]]])
參數:
* **$time**?(int) -- UNIX timestamp
* **$timezone**?(string) -- Timezone
* **$dst**?(bool) -- Whether DST is active
返回: UNIX timestamp
返回類型: int
根據指定的時區和 DST (夏令時,Daylight Saving Time) 將 GMT 時間轉換為本地時間。
例如:
~~~
$timestamp = 1140153693;
$timezone = 'UM8';
$daylight_saving = TRUE;
echo gmt_to_local($timestamp, $timezone, $daylight_saving);
~~~
注解
時區列表請參見本頁末尾。
mysql_to_unix([$time = ''])
參數:
* **$time**?(string) -- MySQL timestamp
返回: UNIX timestamp
返回類型: int
將 MySQL 時間戳轉換為 UNIX 時間戳。
例如:
~~~
$unix = mysql_to_unix('20061124092345');
~~~
unix_to_human([$time = ''[,?$seconds = FALSE[,?$fmt = 'us']]])
參數:
* **$time**?(int) -- UNIX timestamp
* **$seconds**?(bool) -- Whether to show seconds
* **$fmt**?(string) -- format (us or euro)
返回: Formatted date
返回類型: string
將 UNIX 時間戳轉換為方便人類閱讀的格式,如下:
~~~
YYYY-MM-DD HH:MM:SS AM/PM
~~~
這在當你需要在一個表單字段中顯示日期時很有用。
格式化后的時間可以帶也可以不帶秒數,也可以設置成歐洲或美國時間格式。 如果只指定了一個時間參數,將使用不帶秒數的美國時間格式。
例如:
~~~
$now = time();
echo unix_to_human($now); // U.S. time, no seconds
echo unix_to_human($now, TRUE, 'us'); // U.S. time with seconds
echo unix_to_human($now, TRUE, 'eu'); // Euro time with seconds
~~~
human_to_unix([$datestr = ''])
參數:
* **$datestr**?(int) -- Date string
返回: UNIX timestamp or FALSE on failure
返回類型: int
該函數和?[unix_to_human()](http://codeigniter.org.cn/user_guide/helpers/date_helper.html#unix_to_human "unix_to_human")?函數相反,將一個方便人類閱讀的時間格式轉換為 UNIX 時間戳。 這在當你需要在一個表單字段中顯示日期時很有用。如果輸入的時間不同于上面的格式,函數返回 FALSE 。
例如:
~~~
$now = time();
$human = unix_to_human($now);
$unix = human_to_unix($human);
~~~
nice_date([$bad_date = ''[,?$format = FALSE]])
參數:
* **$bad_date**?(int) -- The terribly formatted date-like string
* **$format**?(string) -- Date format to return (same as PHP's?date()?function)
返回: Formatted date
返回類型: string
該函數解析一個沒有格式化過的數字格式的日期,并將其轉換為格式化的日期。它也能解析格式化好的日期。
默認該函數將返回 UNIX 時間戳,你也可以提供一個格式化字符串給第二個參數(和 PHP 的?date()?函數一樣)。
例如:
~~~
$bad_date = '199605';
// Should Produce: 1996-05-01
$better_date = nice_date($bad_date, 'Y-m-d');
$bad_date = '9-11-2001';
// Should Produce: 2001-09-11
$better_date = nice_date($bad_date, 'Y-m-d');
~~~
timespan([$seconds = 1[,?$time = ''[,?$units = '']]])
參數:
* **$seconds**?(int) -- Number of seconds
* **$time**?(string) -- UNIX timestamp
* **$units**?(int) -- Number of time units to display
返回: Formatted time difference
返回類型: string
將一個 UNIX 時間戳轉換為以下這種格式:
~~~
1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes
~~~
第一個參數為一個 UNIX 時間戳,第二個參數是一個比第一個參數大的 UNIX 時間戳。 第三個參數可選,用于限制要顯示的時間單位個數。
如果第二個參數為空,將使用當前時間。
這個函數最常見的用途是,顯示從過去某個時間點到當前時間經過了多少時間。
例如:
~~~
$post_date = '1079621429';
$now = time();
$units = 2;
echo timespan($post_date, $now, $units);
~~~
注解
該函數生成的本文可以在語言文件?language//date_lang.php?中找到。
days_in_month([$month = 0[,?$year = '']])
參數:
* **$month**?(int) -- a numeric month
* **$year**?(int) -- a numeric year
返回: Count of days in the specified month
返回類型: int
返回指定某個月的天數,會考慮閏年。
例如:
~~~
echo days_in_month(06, 2005);
~~~
如果第二個參數為空,將使用今年。
注解
該函數其實是原生的?cal_days_in_month()?函數的別名,如果它可用的話。
date_range([$unix_start = ''[,?$mixed = ''[,?$is_unix = TRUE[,?$format = 'Y-m-d']]]])
參數:
* **$unix_start**?(int) -- UNIX timestamp of the range start date
* **$mixed**?(int) -- UNIX timestamp of the range end date or interval in days
* **$is_unix**?(bool) -- set to FALSE if $mixed is not a timestamp
* **$format**?(string) -- Output date format, same as in?date()
返回: An array of dates
返回類型: array
返回某一段時間的日期列表。
例如:
~~~
$range = date_range('2012-01-01', '2012-01-15');
echo "First 15 days of 2012:";
foreach ($range as $date)
{
echo $date."\n";
}
~~~
timezones([$tz = ''])
參數:
* **$tz**?(string) -- A numeric timezone
返回: Hour difference from UTC
返回類型: int
根據指定的時區(可用的時區列表參見下文的 "時區參考")返回它的 UTC 時間偏移。
例如:
~~~
echo timezones('UM5');
~~~
這個函數和?[timezone_menu()](http://codeigniter.org.cn/user_guide/helpers/date_helper.html#timezone_menu "timezone_menu")?函數一起使用時很有用。
timezone_menu([$default = 'UTC'[,?$class = ''[,?$name = 'timezones'[,?$attributes = '']]]])
參數:
* **$default**?(string) -- Timezone
* **$class**?(string) -- Class name
* **$name**?(string) -- Menu name
* **$attributes**?(mixed) -- HTML attributes
返回: HTML drop down menu with time zones
返回類型: string
該函數用于生成一個時區下拉菜單,像下面這樣。
? ?? ?? ?(UTC -12:00) Baker/Howland Island? ?? ?? ?(UTC -11:00) Samoa Time Zone, Niue? ?? ?? ?(UTC -10:00) Hawaii-Aleutian Standard Time, Cook Islands, Tahiti? ?? ?? ?(UTC -9:30) Marquesas Islands? ?? ?? ?(UTC -9:00) Alaska Standard Time, Gambier Islands? ?? ?? ?(UTC -8:00) Pacific Standard Time, Clipperton Island? ?? ?? ?(UTC -7:00) Mountain Standard Time? ?? ?? ?(UTC -6:00) Central Standard Time? ?? ?? ?(UTC -5:00) Eastern Standard Time, Western Caribbean Standard Time? ?? ?? ?(UTC -4:30) Venezuelan Standard Time? ?? ?? ?(UTC -4:00) Atlantic Standard Time, Eastern Caribbean Standard Time? ?? ?? ?(UTC -3:30) Newfoundland Standard Time? ?? ?? ?(UTC -3:00) Argentina, Brazil, French Guiana, Uruguay? ?? ?? ?(UTC -2:00) South Georgia/South Sandwich Islands? ?? ?? ?(UTC -1:00) Azores, Cape Verde Islands? ?? ?? ?(UTC) Greenwich Mean Time, Western European Time? ?? ?? ?(UTC +1:00) Central European Time, West Africa Time? ?? ?? ?(UTC +2:00) Central Africa Time, Eastern European Time, Kaliningrad Time? ?? ?? ?(UTC +3:00) Moscow Time, East Africa Time? ?? ?? ?(UTC +3:30) Iran Standard Time? ?? ?? ?(UTC +4:00) Azerbaijan Standard Time, Samara Time? ?? ?? ?(UTC +4:30) Afghanistan? ?? ?? ?(UTC +5:00) Pakistan Standard Time, Yekaterinburg Time? ?? ?? ?(UTC +5:30) Indian Standard Time, Sri Lanka Time? ?? ?? ?(UTC +5:45) Nepal Time? ?? ?? ?(UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time? ?? ?? ?(UTC +6:30) Cocos Islands, Myanmar? ?? ?? ?(UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam? ?? ?? ?(UTC +8:00) Australian Western Standard Time, Beijing Time, Irkutsk Time? ?? ?? ?(UTC +8:45) Australian Central Western Standard Time? ?? ?? ?(UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk Time? ?? ?? ?(UTC +9:30) Australian Central Standard Time? ?? ?? ?(UTC +10:00) Australian Eastern Standard Time, Vladivostok Time? ?? ?? ?(UTC +10:30) Lord Howe Island? ?? ?? ?(UTC +11:00) Srednekolymsk Time, Solomon Islands, Vanuatu? ?? ?? ?(UTC +11:30) Norfolk Island? ?? ?? ?(UTC +12:00) Fiji, Gilbert Islands, Kamchatka Time, New Zealand Standard Time? ?? ?? ?(UTC +12:45) Chatham Islands Standard Time? ?? ?? ?(UTC +13:00) Phoenix Islands Time, Tonga? ?? ?? ?(UTC +14:00) Line Islands? ? ?
當你的站點允許用戶選擇自己的本地時區時,這個菜單會很有用。
第一個參數為菜單默認選定的時區,例如,要設置太平洋時間為默認值,你可以這樣:
~~~
echo timezone_menu('UM8');
~~~
菜單中的值請參見下面的時區參考。
第二個參數用于為菜單設置一個 CSS 類名。
第四個參數用于為生成的 select 標簽設置一個或多個屬性。
注解
菜單中的文本可以在語言文件?language//date_lang.php?中找到。
## [時區參考](http://codeigniter.org.cn/user_guide/helpers/date_helper.html#id8)
下表列出了每個時區和它所對應的位置。
注意,為了表述清晰和格式工整,有些位置信息做了適當的刪減。
| 時區 | 位置 |
| --- | --- |
| UM12 | (UTC - 12:00) 貝克島、豪蘭島 |
| UM11 | (UTC - 11:00) 薩摩亞時區、紐埃 |
| UM10 | (UTC - 10:00) 夏威夷-阿留申標準時間、庫克群島 |
| UM95 | (UTC - 09:30) 馬克薩斯群島 |
| UM9 | (UTC - 09:00) 阿拉斯加標準時間、甘比爾群島 |
| UM8 | (UTC - 08:00) 太平洋標準時間、克利珀頓島 |
| UM7 | (UTC - 07:00) 山區標準時間 |
| UM6 | (UTC - 06:00) 中部標準時間 |
| UM5 | (UTC - 05:00) 東部標準時間、西加勒比 |
| UM45 | (UTC - 04:30) 委內瑞拉標準時間 |
| UM4 | (UTC - 04:00) 大西洋標準時間、東加勒比 |
| UM35 | (UTC - 03:30) 紐芬蘭標準時間 |
| UM3 | (UTC - 03:00) 阿根廷、巴西、法屬圭亞那、烏拉圭 |
| UM2 | (UTC - 02:00) 南喬治亞島、南桑威奇群島 |
| UM1 | (UTC -1:00) 亞速爾群島、佛得角群島 |
| UTC | (UTC) 格林尼治標準時間、西歐時間 |
| UP1 | (UTC +1:00) 中歐時間、西非時間 |
| UP2 | (UTC +2:00) 中非時間、東歐時間 |
| UP3 | (UTC +3:00) 莫斯科時間、東非時間 |
| UP35 | (UTC +3:30) 伊朗標準時間 |
| UP4 | (UTC +4:00) 阿塞拜疆標準時間、薩馬拉時間 |
| UP45 | (UTC +4:30) 阿富汗 |
| UP5 | (UTC +5:00) 巴基斯坦標準時間、葉卡捷琳堡時間 |
| UP55 | (UTC +5:30) 印度標準時間、斯里蘭卡時間 |
| UP575 | (UTC +5:45) 尼泊爾時間 |
| UP6 | (UTC +6:00) 孟加拉國標準時間、不丹時間、鄂木斯克時間 |
| UP65 | (UTC +6:30) 可可島、緬甸 |
| UP7 | (UTC +7:00) 克拉斯諾亞爾斯克時間、柬埔寨、老撾、泰國、越南 |
| UP8 | (UTC +8:00) 澳大利亞西部標準時間、北京時間 |
| UP875 | (UTC +8:45) 澳大利亞中西部標準時間 |
| UP9 | (UTC +9:00) 日本標準時間、韓國標準時間、雅庫茨克 |
| UP95 | (UTC +9:30) 澳大利亞中部標準時間 |
| UP10 | (UTC +10:00) 澳大利亞東部標準時間、海參崴時間 |
| UP105 | (UTC +10:30) 豪勛爵島 |
| UP11 | (UTC +11:00) 中科雷姆斯克時間、所羅門群島、瓦努阿圖 |
| UP115 | (UTC +11:30) 諾福克島 |
| UP12 | (UTC +12:00) 斐濟、吉爾伯特群島、堪察加半島、新西蘭 |
| UP1275 | (UTC +12:45) 查塔姆群島標準時間 |
| UP13 | (UTC +13:00) 鳳凰島、湯加 |
| UP14 | (UTC +14:00) 萊恩群島 |
- 歡迎使用 CodeIgniter
- 安裝說明
- 下載 CodeIgniter
- 安裝說明
- 從老版本升級
- 疑難解答
- CodeIgniter 概覽
- CodeIgniter 將從這里開始
- CodeIgniter 是什么?
- 支持特性
- 應用程序流程圖
- 模型-視圖-控制器
- 設計與架構目標
- 教程 - 內容提要
- 加載靜態內容
- 讀取新聞條目
- 創建新聞條目
- 結束語
- 常規主題
- CodeIgniter URL
- 控制器
- 保留名稱
- 視圖
- 模型
- 輔助函數
- 使用 CodeIgniter 類庫
- 創建類庫
- 使用 CodeIgniter 驅動器
- 創建驅動器
- 創建核心系統類
- 創建附屬類
- 鉤子 - 擴展框架核心
- 自動加載資源
- 公共函數
- 兼容性函數
- URI 路由
- 錯誤處理
- 網頁緩存
- 程序分析
- 以 CLI 方式運行
- 管理你的應用程序
- 處理多環境
- 在視圖文件中使用 PHP 替代語法
- 安全
- PHP 開發規范
- 類庫參考
- 基準測試類
- 緩存驅動器
- 日歷類
- 購物車類
- 配置類
- Email 類
- 加密類
- 加密類(新版)
- 文件上傳類
- 表單驗證類
- FTP 類
- 圖像處理類
- 輸入類
- Javascript 類
- 語言類
- 加載器類
- 遷移類
- 輸出類
- 分頁類
- 模板解析類
- 安全類
- Session 類
- HTML 表格類
- 引用通告類
- 排版類
- 單元測試類
- URI 類
- 用戶代理類
- XML-RPC 與 XML-RPC 服務器類
- Zip 編碼類
- 數據庫參考
- 數據庫快速入門: 示例代碼
- 數據庫配置
- 連接你的數據庫
- 查詢
- 生成查詢結果
- 查詢輔助函數
- 查詢構造器類
- 事務
- 數據庫元數據
- 自定義函數調用
- 數據庫緩存類
- 數據庫工廠類
- 數據庫工具類
- 數據庫驅動器參考
- 輔助函數參考
- 數組輔助函數
- 驗證碼輔助函數
- Cookie 輔助函數
- 日期輔助函數
- 目錄輔助函數
- 下載輔助函數
- 郵件輔助函數
- 文件輔助函數
- 表單輔助函數
- HTML 輔助函數
- 語言輔助函數
- Inflector 輔助函數
- 數字輔助函數
- 路徑輔助函數
- 安全輔助函數
- 表情輔助函數
- 字符串輔助函數
- 文本輔助函數
- 排版輔助函數
- URL 輔助函數
- XML 輔助函數
- 向 CodeIgniter 貢獻你的力量