PHP日期時間函式的高階應用技巧

2021-10-09 01:20:40 字數 3528 閱讀 8558

php的日期時間函式date()中介紹了php日期時間函式的簡單用法,這類將介紹更多的函式來豐富我們的應用。

checkdate($month,$date,$year)

如果應用的值構成乙個有效日期,則該函式返回為真。例如,對於錯誤日期2023年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時間2023年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++)

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

?>

PHP高階(第九彈 日期時間函式)

我們可以先看看php是否已經有了日期時間庫的擴充套件 phpinfo 執行該檔案,可以搜到,date time support是enabled的,說明已經有這個擴充套件了。在phpinfo 中可以看到,預設的時區default timezone為utc。可以使用date default timezo...

PHP 日期時間函式

1.設定時區 a 修改php配置檔案date.timezone設定時區 b 通過date default timezone set 動態設定時區 c 通過ini set 動態設定時區 asia shanghai 或者prc date default timezone set prc 2.date 函...

PHP應用日期與時間

時間戳 1.是乙個整數 2.1970 1 1 到現在的秒數 1213212121 2014 02 14 11 11 11 02 14 2014 11 11 11 date default timezone set prc start microtime true for i 0 i 100000 i...