• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • idate()

    (PHP 5, PHP 7)

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

    说明

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

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

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

    $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以秒为单位的时区偏移量
    Note:

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

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

    参见date()和time()。

    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>";
    ?>
    

    上篇:gmstrftime()

    下篇:localtime()