idate()

1. 定义

该函数接受一个格式化字符来将本地日期时间格式化为一个整数。 返回被格式化后的整数。

2. 语法

idate ( string $format [, int $timestamp ] ) : int

3. 参数说明

参数 可选性 数据类型 描述
$format 必需 字符串 符合规范的解析日期/时间的格式化字符串
$timestamp 可选 整型 整型的时间戳,默认为当前时间(time()

其中,$format参数有以下几种规则:

字符 说明
B 互联网时间
L 是否为闰年,是则返回 1,否则返回 0
y 年份(1 或 2 位数字,当两位数的年份以0开头时,开头的0会被忽略)
Y 年份(4 位数字)
m 月份的数字
d 月份中的第几天
h 小时(12 小时格式)
H 小时(24 小时格式)
i 分钟
s 秒数
z 年份中的第几天
w 星期中的第几天(星期天是 0)
t 本月的总天数
W ISO-8601 格式年份中的第几个星期,每星期从星期一开始
Z 以秒为单位的时区偏移量
U 自 Unix 纪元(January 1 1970 00:00:00 GMT)起的秒数——这和time()作用相同
I 如果启用夏时制则返回 1,否则返回 0

4. 示例

<?php

// idate()
// 将本地日期时间格式化为一个整数

$res = idate('Y', time());
var_dump($res);// 输出:int(2019)

$res = idate('y', strtotime('2001-08-15'));
var_dump($res);// 输出:int(1):原应是 01,但作为整型即为 1 

// $res = idate('Y-m', strtotime('2001-08-15'));
// 报错:Warning: idate(): idate format is one char

$res = idate('w', strtotime('2001-08-15'));
var_dump($res);// 输出:int(3)