|
如果應(yīng)用的值構(gòu)成一個(gè)有效日期,則該函數(shù)返回為真。例如,對于錯(cuò)誤日期2005年2月31日,此函數(shù)返回為假。
在日期用于計(jì)算或保存在數(shù)據(jù)庫中之前,可用此函數(shù)檢查日期并使日期生效。
<?php
// returns false
echo checkdate(2,30,2005) ? "valid" : "invalid";
// returns true
echo checkdate(4,6,2010) ? "valid" : "invalid";
?>
getdate($ts)
在沒有自變量的情況下,該函數(shù)以結(jié)合數(shù)組的方式返回當(dāng)前日期與時(shí)間。數(shù)組中的每個(gè)元素代表日期/時(shí)間值中的一個(gè)特定組成部分。可向函數(shù)提交可選的時(shí)間標(biāo)簽自變量,以獲得與時(shí)間標(biāo)簽對應(yīng)的日期/時(shí)間值。
應(yīng)用此函數(shù)來獲得一系列離散的,容易分離的日期/時(shí)間值。
<?php // get date as associative array $arr = getdate(); echo "Date is " . $arr['mday'] . " " . $arr['weekday'] . " " . $arr['year']; echo "Time is " . $arr['hours'] . ":" . $arr['minutes']; ?> |
mktime($hour, $minute, $second, $month, $day, $year)
此函數(shù)的作用與getdate()的作用相反:它由一系列的日期與時(shí)間值生成一個(gè)UNIX時(shí)間標(biāo)簽(GMT時(shí)間1970年1月1日到現(xiàn)在消逝的秒數(shù))。不用自變量時(shí),它生成當(dāng)前時(shí)間的UNIX時(shí)間標(biāo)簽。
用此函數(shù)獲得即時(shí)時(shí)間的UNIX時(shí)間標(biāo)簽。這種時(shí)間標(biāo)簽通常用于許多數(shù)據(jù)庫與程序語言中。
<?php // returns timestamp for 13:15:23 7-Jun-2006 echo mktime(13,15,23,6,7,2006); ?> |
date($format, $ts)
此函數(shù)將UNIX時(shí)間標(biāo)簽格式化成一個(gè)可人為閱讀的日期字符串。它是php日期/時(shí)間API中功能最為強(qiáng)大的函數(shù),可用在一系列的修正值中,將整數(shù)時(shí)間標(biāo)簽轉(zhuǎn)變?yōu)樗璧淖址袷健?br>
為顯示格式化時(shí)間或日期時(shí),應(yīng)用此函數(shù)。
<?php // format current date // returns "13-Sep-2005 01:16 PM" echo date("d-M-Y h:i A", mktime()); ?> |
strtotime($str)
此函數(shù)將可人為閱讀的英文日期/時(shí)間字符串轉(zhuǎn)換成UNIX時(shí)間標(biāo)簽。
應(yīng)用此函數(shù)將非標(biāo)準(zhǔn)化的日期/時(shí)間字符串轉(zhuǎn)換成標(biāo)準(zhǔn)、兼容的UNIX時(shí)間標(biāo)簽。
<?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()函數(shù)定義的那樣,此函數(shù)將UNIX時(shí)間標(biāo)簽格式化成適用于當(dāng)前環(huán)境的日期字符串。
應(yīng)用此函數(shù)建立與當(dāng)前環(huán)境兼容的日期字符串。
<?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()
如前面的setlocale()函數(shù)定義的那樣,此函數(shù)將UNIX時(shí)間標(biāo)簽格式化成適用于當(dāng)前環(huán)境的日期字符串。
應(yīng)用此函數(shù)建立與當(dāng)前環(huán)境兼容的日期字符串。
<?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)
此函數(shù)由一系列用GMT時(shí)間表示的日期與時(shí)間值生成一個(gè)UNIX時(shí)間標(biāo)簽。不用自變量時(shí),它生成一個(gè)當(dāng)前GMT即時(shí)時(shí)間的UNIX時(shí)間標(biāo)簽。
用此函數(shù)來獲得GMT即時(shí)時(shí)間的UNIX時(shí)間標(biāo)簽。
<?php // returns timestamp for 12:25:23 9-Jul-2006 echo gmmktime(12,25,23,7,9,2006); ?> |
gmdate($format, $ts)
此函數(shù)將UNIX時(shí)間標(biāo)簽格式化成可人為閱讀的日期字符串。此日期字符串以GMT(非當(dāng)?shù)貢r(shí)間)表示。
用GMT表示時(shí)間標(biāo)簽時(shí)應(yīng)用此函數(shù)。
<?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)、date_default_timezone_get()
此函數(shù)此后所有的日期/時(shí)間函數(shù)調(diào)用設(shè)定并恢復(fù)默認(rèn)的時(shí)區(qū)。
注:此函數(shù)僅在php 5.1+中有效。
此函數(shù)是一個(gè)方便的捷徑,可為以后的時(shí)間操作設(shè)定時(shí)區(qū)。
<?php // set timezone to UTC date_default_timezone_set('UTC'); ?> |
php技術(shù):PHP日期時(shí)間函數(shù)的高級應(yīng)用技巧,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。