• 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
  • 相关阅读:
    浅拷贝在项目中的应用
    MVC3中使用AuthorizeAttribute特性来完成登陆权限的验证
    一个面向对象的JS例子,很好的支持了开闭原则(不要重复你昨天的代码)
    c++学习笔记5
    JS跨域访问操作iframe
    Select函数
    MacOS10.8.3+Xcode4.6+IOS6.1 编译FFmpeg,简单使用
    eclipse部署,在tomcat中找不到eclipse发布的项目。eclipse更改项目发布路径
    初识Volley(二)
    MySQL 5.0 迁移到 MariaDB 10.0.2 存储过程无法迁移
  • 原文地址:https://www.cnblogs.com/zhongJaywang/p/5889331.html
Copyright © 2020-2023  润新知