zoukankan      html  css  js  c++  java
  • PHP的时间总结

    PHP下的时间函数和类型比较多,经常容易混淆,今天花时间整理一下。

    一、主要的时间相关概念

    1.DateTime: 代表日期和时间

    2.DateTimeInterface:定义DateTime的主要接口

    3.DateInterval:代表时间的间隔

    4.TimeStamp:时间戳

    5.DateTimeZone:时区,通过修改apache下的php.ini实现

    [Date]
    date.timezone = Asia/Shanghai
    

     二、主要的时间函数

    1.time:返回unix的时间戳,自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数,mktime已经不建议使用

    <?php
    //一周后时间戳
    $nextWeek = time() + (7 * 24 * 60 * 60); 
    //当前时间字符串
    echo 'Now:       '. date('Y-m-d') ."
    ";
    echo 'Next Week: '. date('Y-m-d', $nextWeek) ."
    "; 
    echo 'Next Week: '. date('Y-m-d', strtotime('+1 week')) ."
    "; ?>

    2.date:返回格式化的时间

    date('Y年m月d日') //2013年07月26日

     3.strtotime:返回时间戳格式

    4.date_diff:时间比较函数,返回DateInterval类型

    $d1 = new DateTime('2011-08-05');
    $d2 = new DateTime('now');
    print_r($d2->diff($d1));
    

    三、常见示例

    1.返回当前时间:

    new DateTime('now')      //返回当前时间的DateTime类型
    time()                   //返回当前时间的时间戳
    strtotime('+0day')       //返回当前时间的时间戳
    date('Y-m-d')            //返回当前时间的字符串格式
  • 相关阅读:
    Selenium IDE安装及环境搭建教程
    菜鸟学自动化测试(一)----selenium IDE
    seleniumIDE回放找不到页面元素
    selenium报错汇总
    利用saltstack初始化OpenStack服务器环境
    工作机会
    使用state模块部署lamp架构
    tar命令
    svn备份
    MAC电脑密码破解
  • 原文地址:https://www.cnblogs.com/xianyun/p/3217468.html
Copyright © 2011-2022 走看看