zoukankan      html  css  js  c++  java
  • PHP时间日期

    • PHP常用的几个时间日期函数有:date(),mktime(),strtotime();

      一、string date ( string $format [, int $timestamp ] )

      函数作用:格式化一个本地时间/日期

      date() 函数的第二个参数规定了一个时间戳。此参数是可选的。如果您没有提供时间戳,当前的时间将被使用。 

       下面列出了一些常用于日期的字符:

      • d - 表示月里的某天(01-31)
      • m - 表示月(01-12)
      • Y - 表示年(四位数)
      • 1 - 表示周里的某天

       下面是常用于时间的字符:

      • h - 带有首位零的 12 小时小时格式
      • i - 带有首位零的分钟
      • s - 带有首位零的秒(00 -59)
      • a - 小写的午前和午后(am 或 pm)
    echo date('Y-m-d H:i:s',time());
    //输出:2016-09-20 15:52:51
    
    echo date('Y-m-d H:i:s');
    //输出:2016-09-20 15:52:51 date()的第二个参数规定了一个时间戳,此参数是可选的,在这里没有提供,当前的时间将被使用

      二、int strtotime ( string $time [, int $now = time() ] )

      参数:time:日期/时间字符串;now:用来计算返回值的时间戳;

      函数作用:将任何英文文本的日期时间描述解析为 Unix 时间戳

    //PHP 在将字符串转换为日期这方面非常聪明,所以您能够使用各种值:
    
    
    //----------------------------------------------
    
    $d=strtotime("tomorrow");
    echo date("Y-m-d h:i:sa", $d) . "<br>";
    
    $d=strtotime("next Saturday");
    echo date("Y-m-d h:i:sa", $d) . "<br>";
    
    $d=strtotime("+3 Months");
    echo date("Y-m-d h:i:sa", $d) . "<br>";
    //以上三个例子strtotime()是没有第二个参数时间戳的,此时默认为当前时间
    
    $temp=strtotime('2016-09-22');
    
    $d=strtotime('tomorrow',$temp);
    echo date('Y-m-d',$d);
    
    //输出:2016-09-23
    //-----------------------------------------------

      三、int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n")[, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )

      参数:时,分,秒,月,日,年,is_dst

         函数作用:取得一个日期的 Unix 时间戳

      

    $d=mktime(9, 12, 31, 6, 10, 2015);
    echo  date("Y-m-d h:i:sa", $d);
    
    //输出:2015-06-10 09:12:31am
  • 相关阅读:
    java笔记使用线程池优化多线程编程
    java笔记查看和修改线程名称
    java笔记查看和修改线程的优先级
    java笔记策略模式和简单工厂模式
    java笔记用ThreadLocal管理线程,Callable<V>接口实现有返回值的线程
    java笔记枚举总结与详解
    java笔记关于克隆技术
    java笔记反射机制之基础总结与详解
    java笔记使用事件分配线程更新Swing控件
    java笔记关于int和byte[]的转换
  • 原文地址:https://www.cnblogs.com/zhongJaywang/p/5889331.html
Copyright © 2011-2022 走看看