国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP日期時間函數的高級應用技巧

2024-05-04 23:03:36
字體:
來源:轉載
供稿:網友
詳細講解php的日期時間函數date()中介紹了php日期時間函數的簡單用法,這類將介紹更多的函數來豐富我們的應用。

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
// 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)

此函數的作用與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()

如前面的setlocale()函數定義的那樣,此函數將unix時間標簽格式化成適用于當前環境的日期字符串。

應用此函數建立與當前環境兼容的日期字符串。

<?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)、date_default_timezone_get()

此函數此后所有的日期/時間函數調用設定并恢復默認的時區。

注:此函數僅在php 5.1+中有效。

此函數是一個方便的捷徑,可為以后的時間操作設定時區。

<?php
// set timezone to utc
date_default_timezone_set('utc');
?>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 房山区| 台南市| 乌鲁木齐县| 商水县| 龙江县| 诏安县| 武夷山市| 万山特区| 靖远县| 高阳县| 苗栗县| 班戈县| 黔南| 集安市| 博爱县| 通化县| 正宁县| 郑州市| 怀集县| 修文县| 剑阁县| 广河县| 中宁县| 沭阳县| 永新县| 肇州县| 阳山县| 翁源县| 奈曼旗| 临武县| 类乌齐县| 武宁县| 平舆县| 台湾省| 沾益县| 谢通门县| 屏边| 乌拉特中旗| 普格县| 韶山市| 巴里|