idate

(PHP 5)

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

说明

int idate ( string format [, int timestamp] )

根据给定的格式字符对 timestamp 格式化并返回数字结果。timestamp 为可选项,默认值为本地当前时间,即 time() 的值。

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

表格 1. format 参数可识别以下字符

format 字符说明
BSwatch Beat/Internet Time
d月份中的第几天
h小时(12 小时格式)
H小时(24 小时格式)
i分钟
I如果启用夏时制则返回 1,否则返回 0
L如果是闰年则返回 1,否则返回 0
m月份的数字
s秒数
t本月的总天数
U自 Unix 纪元(January 1 1970 00:00:00 GMT)起的秒数――这和 time() 作用相同
w星期中的第几天(星期天是 0
WISO-8601 格式年份中的第几个星期,每星期从星期一开始
y年份(1 或 2 位数字――见下面说明)
Y年份(4 位数字)
z年份中的第几天
Z以秒为单位的时区偏移量

注: 因为 idate() 总是返回 integer,不能以“0”开头,因此 idate() 可能会返回比用户期望中要少的数字。见下面例子:

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

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

参见 date()time()


add a note add a note User Contributed Notes
egingell at sisna dot com
10-Oct-2006 07:26
<?

// idate() for PHP < 5

// Sanity check
if (!is_callable('idate')) {
   function
idate($char, $ts = false) {
       if (
$ts === false) {
          
$ts = time();
       } else if (!
is_numeric($ts)) {
           return
false;
       }
      
$char = $char{0};
       if (
$char == 'B') {
          
// Swatch time ignores the $ts argument.
          
return ((int) ((gmdate('U') + 3600) * (1000 / 86400))) % 1000;
       } else {
           return (int)
date($char, $ts); // Drop leading zeroes by casting into an integer.
      
}
   }
}

?>
qjoe dot sb at gmail dot com
08-Jun-2005 02:21
To clarify an ambiguity due to the fount: 'I' is a capital i, rather than a miniscule L.