idate

(PHP 5, PHP 7, PHP 8)

idate将本地日期/时间格式化为整数

说明

idate(string $format, ?int $timestamp = null): int|false

使用明确的 int 类型 timestamp 或者当前本地时间(如果没有明确时间戳),根据明确的格式化字符串返回格式化后的数字。换句话说,timestamp 是可选的,默认值是 time() 的值。

date() 不同,idate() 只接受一个字符作为 format 的参数。

参数

format

format 参数字符串仅识别以下字符
format 字符 说明
B Swatch Beat/互联网时间
d 月份中的第几天
h 小时(12 小时制)
H 小时(24 小时制)
i 分钟
I(大写 i) 如果启用夏令时则返回 1,否则返回 0
L(大写 l) 如果是闰年则返回 1,否则返回 0
m 月份的数字
N ISO-8601 格式的周几(1 表示周一到 7 表示周日)
o ISO-8601 格式的年份(4位)
s 秒数
t 本月的总天数
U 自 Unix 纪元(January 1 1970 00:00:00 GMT)起的秒数——这和 time() 相同
w 周几(0 是周日)
W ISO-8601 格式的年份中第几个星期,每星期从星期一开始
y 年份(1 或 2 位数字——见下面说明)
Y 年份(4 位数字)
z 年份中的第几天
Z 以秒为单位的时差

timestamp

可选的 timestamp 参数是一个 int 的 Unix 时间戳,如未指定或是 null,参数值默认为当前本地时间。也就是说,其值默认为 time() 的返回值。

返回值

成功时返回 int, 或者在失败时返回 false

由于 idate() 始终返回不以“0”开头的 int,因此 idate() 返回的数字可能位数要比期望的小。查看以下示例。

错误/异常

在每次调用日期/时间函数时,如果时区无效则会引发 E_NOTICE 错误。参见 date_default_timezone_set()

更新日志

版本 说明
8.2.0 添加 N(ISO-8601 格式的周几)和 o(ISO-8601 格式的年份)格式字符。
8.0.0 现在 timestamp 允许为 null。

示例

示例 #1 idate() example

<?php
$timestamp
= strtotime('1st January 2004'); //1072915200

// 下面以两位数字格式打印年份,但是因为
// 以“0”打头,因此只会打印“4”
echo idate('y', $timestamp);
?>

参见

add a note add a note

User Contributed Notes 1 note

up
-6
khaine at herbok dot org
13 years ago
A function that made by idate for print a hour you want:

<?php
function hour ( $a, $b )
{
   
$timestamp = strtotime(''.$a.':'.$b.':00');
   
$aa = idate('H', $timestamp);
   
$bb = idate('i', $timestamp);
    if(
$bb=="0") { $cc = "00"; } else { $cc = $bb; }
   
$dd = $aa.".".$cc;
    return(
$dd);
}
?>

Why should i use it?
For example:
You have to print 08:00 to 21:00 by 15 minutes periods.
This is a useful code for shipping sites for delivery time.

<?php
echo "<select name=\"example\">";
for(
$i=8;$i<=20;$i++)
{
    for(
$ii=0;$ii<=3;$ii++)
    {
       
$iii = $ii * 15;
       
$hour = hour($i, $iii);
        echo
"<option value=\"".$hour."\">".$hour."</option>";
    }
}
echo
"</select>";
?>
To Top