| 功能 |
說明 |
執行個體 |
| checkdate ($month, ?date, $year) |
如果應用的值構成一個有效日期,則該函數返回為真。例如,對於錯誤日期2005年2月31日,此函數返回為假。 在日期用於計算或儲存在資料庫中之前,可用此函數檢查日期並使日期生效。 |
<?php // returns false echo checkdate(2,30,2005) ? "valid" : "invalid"; // returns true echo checkdate(4,6,2010) ? "valid" : "invalid"; ?> |
| getdate($ts) |
在沒有自變數的情況下,該函數以結合數組的方式返回當前日期與時間。數組中的每個元素代表日期/時間值中的一個特定組成部分。可向函數提交可選的時間標籤自變數,以獲得與時間標籤對應的日期/時間值。 應用此函數來獲得一系列離散的,容易分離的日期/時間值。 |
<?php // returns timestamp for 13:15:23 7-Jun-2006 echo mktime(13,15,23,6,7,2006); ?> |
| mktime($hour, $minute, $second, $month, $day, $year) |
此函數的作用與getdate()的作用相反:它由一系列的日期與時間值產生一個UNIX時間標籤(GMT時間1970年1月1日到現在消逝的秒數)。不用自變數時,它產生目前時間的UNIX時間標籤。 用此函數獲得即時時間的UNIX時間標籤。這種時間標籤通常用於許多資料庫與程式語言中。 |
<?php // returns timestamp for 13:15:23 7-Jun-2006 echo mktime(13,15,23,6,7,2006); ?> |
| date($format, $ts) |
此函數將UNIX時間標籤格式化成一個可人為閱讀的日期文字。它是PHP日期/時間API中功能最為強大的函數,可用在一系列的修正值中,將整數時間標籤轉變為所需的字串格式。 為顯示格式化時間或日期時,應用此函數。 |
<?php // format current date // returns "13-Sep-2005 01:16 PM" echo date("d-M-Y h:i A", mktime ()); ?> |
| strtotime ($str) |
此函數將可人為閱讀的英文日期/時間字串轉換成UNIX時間標籤。 應用此函數將非標準化的日期/時間字串轉換成標準、相容的UNIX時間標籤。 |
<?php // returns 13-Sep-05 echo date("d-M-y", strtotime("today")); // returns 14-Sep-05 echo date("d-M-y", strtotime("tomorrow")); // returns 16-Sep-05 echo date("d-M-y", strtotime("today +3 days")); ?> |
| strftime ($format, ?ts) |
如前面的setlocale()函數定義的那樣,此函數將UNIX時間標籤格式化成適用於當前環境的日期文字。 應用此函數建立與當前環境相容的日期文字。 |
<?php // set locale to France (on Windows) setlocale(LC_TIME, "fra_fra"); // format month/day names // as per locale setting // returns "septembre" and "mardi" echo strftime("Month: %B "); echo strftime("Day: %A "); ?> |
| microtime() |
此函數返回GMT時間1970年1月1日到現在所消逝的秒數與微秒數。 在基準特定的代碼塊,準確測量它的執行時間時應用此函數。 |
<?php // get starting value $start = microtime(); // run some code for ($x=0; $x<1000; $x++) { 牋?$null = $x * $x; } // get ending value $end = microtime(); // calculate time taken for code execution echo "Elapsed time: " . ($end - $start) ." sec"; ?> |
| gmmktime ($hour, $minute, $second, $month, $day, $year) |
此函數由一系列用GMT時間表示的日期與時間值產生一個UNIX時間標籤。不用自變數時,它產生一個當前GMT即時時間的UNIX時間標籤。 用此函數來獲得GMT即時時間的UNIX時間標籤。 |
<?php // returns timestamp for 12:25:23 9-Jul-2006 echo gmmktime (12,25,23,7,9,2006); ?> |
| gmdate ($format, $ts) |
此函數將UNIX時間標籤格式化成可人為閱讀的日期文字。此日期文字以GMT(非當地時間)表示。 用GMT表示時間標籤時應用此函數。 |
<?php // format current date into GMT // returns "13-Sep-2005 08:32 AM" echo gmdate("d-M-Y h:i A", mktime()); ?> |
| date_default_ timezone_set ($tz)and date_default_ timezone_get() |
此函數此後所有的日期/時間函數調用設定並恢複預設的時區。 註:此函數僅在PHP 5.1+中有效。 此函數是一個方便的捷徑,可為以後的時間操作設定時區。 |
<?php // set timezone to UTC date_default_timezone_set ('UTC'); ?> |