idate()函數(shù)將本地時(shí)間日期格式化為整數(shù)
idate()函數(shù)接受格式字符串作為參數(shù),以指定的格式格式化本地日期/時(shí)間,然后返回該日期/時(shí)間。
idate($format, [$timestamp])
根據(jù)給定的格式字符對(duì) timestamp 格式化并返回?cái)?shù)字結(jié)果。
timestamp 為可選項(xiàng),默認(rèn)值為本地當(dāng)前時(shí)間,即 time() 的值。 和 date() 不同,idate()只接受一個(gè)字符作為 format 參數(shù)。
| format 字符 | 說明 |
|---|---|
| B | Swatch Beat/Internet Time |
| d | 月份中的第幾天 |
| h | 小時(shí)(12 小時(shí)格式) |
| H | 小時(shí)(24 小時(shí)格式) |
| i | 分鐘 |
| I | 如果啟用夏時(shí)制則返回 1,否則返回 0 |
| L | 如果是閏年則返回 1,否則返回 0 |
| m | 月份的數(shù)字 |
| s | 秒數(shù) |
| t | 本月的總天數(shù) |
| U | 自 Unix 紀(jì)元(January 1 1970 00:00:00 GMT)起的秒數(shù)——這和 time() 作用相同 |
| w | 星期中的第幾天(星期天是 0) |
| W | ISO-8601 格式年份中的第幾個(gè)星期,每星期從星期一開始 |
| y | 年份(1 或 2 位數(shù)字——見下面說明) |
| Y | 年份(4 位數(shù)字) |
| z | 年份中的第幾天 |
| Z | 以秒為單位的時(shí)區(qū)偏移量 |
| 序號(hào) | 參數(shù)及說明 |
|---|---|
| 1 | format(必需) 這是一個(gè)字符串值,表示您需要格式化本地日期/時(shí)間的格式。 |
| 2 | timestamp(可選) 這是一個(gè)整數(shù),表示表示當(dāng)前本地時(shí)間的時(shí)間戳。 |
PHP idate()函數(shù)返回一個(gè)整數(shù)值,該整數(shù)值表示格式化的日期/時(shí)間。
此函數(shù)最初是在PHP 5.0版中引入的,并且可以在所有更高版本中使用。
以下示例演示了idate()函數(shù)的用法-
<?php
$format = "U";
$res = idate($format);
print("Timestamp: ".$res);
?>測試看看?/?輸出結(jié)果
Timestamp: 1589280496
以下示例通過傳遞timestamp參數(shù)來調(diào)用idate()函數(shù)-
<?php
$date = date_create();
$timestamp = date_timestamp_get($date);
$format = "U";
$res = idate($format, $timestamp);
print("時(shí)間戳: ".$res);
?>測試看看?/?輸出結(jié)果
時(shí)間戳: 1589282148
讓我們看看idate()函數(shù)的各種格式字符及其結(jié)果-
<?php
print("B :".idate("B"));
print("\n");
print("d :".idate("d"));
print("\n");
print("h :".idate("h"));
print("\n");
print("H: ".idate("H"));
print("\n");
print("i :".idate("i"));
print("\n");
print("I :".idate("I"));
print("\n");
print("L :".idate("L"));
print("\n");
print("m :".idate("m"));
print("\n");
print( "s :".idate("s"));
print("\n");
print("t :".idate("t"));
print("\n");
print("U :".idate("U"));
print("\n");
print("w :".idate("w"));
print("\n");
print("w:".idate("W"));
print("\n");
print("y :".idate("y"));
print("\n");
print("Y :".idate("Y"));
print("\n");
print("z :".idate("z"));
print("\n");
print("Z :".idate("Z"));
print("\n");
?>測試看看?/?這將產(chǎn)生以下輸出-
B :758 d :18 h :5 H: 17 i :11 I :0 L :1 m :5 s :54 t :31 U :1589821914 w :1 w:21 y :20 Y :2020 z :138 Z :0
<?php
$timestamp = strtotime('1st January 2014');
echo idate('y', $timestamp);
echo"\n";
echo idate('t', $timestamp);
?>測試看看?/?這將產(chǎn)生以下輸出-
14 31