zoukankan      html  css  js  c++  java
  • PHP时间处理

    https://www.cnblogs.com/iAmSoScArEd/

    时间格式说明:

    $time = time(); // 当前时间戳
    var_dump($time);  // int(1516155874)
    
    $time_str = date('Y-m-d H:i:s', $time); // 将时间戳转化为相应的时间字符串
    var_dump($time_str);  // string(19) "2018-01-17 02:24:34"
    
    $time_int = strtotime($time_str);  // 将时间字符串转化为时间戳
    var_dump($time_int); // int(1516155874)

    1、PHP基本常用的时间函数

    date(): 把时间戳格式化为更易读的日期和时间

    time(): 获取当前 Unix 时间戳

    strtotime(): 将表示时间和日期的字符串转化为相应的时间戳

    mktime(): 创建日期

    2、date()函数

    <?php
    // 假定今天时间:March 10th, 2001, 5:16:18 pm
    $today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (MySQL datetime 格式)
    $today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
    $today = date("m.d.y");                         // 03.10.01
    $today = date("j, n, Y");                       // 10, 3, 2001
    $today = date("Ymd");                           // 20010310
    $today = date('h-i-s, j-m-y, it is w Day z ');  // 05-16-17, 10-03-01, 1631 1618 6 Fripm01
    $today = date('i	 is 	he jS day.');   // It is the 10th day.
    $today = date("D M j G:i:s T Y");               // Sat Mar 10 15:16:08 MST 2001
    $today = date('H:m:s m is mo
    	h');     // 17:03:17 m is month
    $today = date("H:i:s");                         // 17:16:17
    
    date('Y'); // 当前年
    date('m'); // 当前月
    date('d'); // 当前日

    3、strtotime()函数

    <?php
    //返回时间戳
    echo strtotime("now"), " ";         // 现在时间戳 echo strtotime("10 September 2000"), " "; // 2000年10月的现在时间的时间戳 echo strtotime("+1 day"), " "; // 距离现在一天后的时间戳 echo strtotime("-3 day"), " ";       // 距离现在三天前的时间戳 echo strtotime("+1 week"), " ";       // 距离现在一周后的时间戳 echo strtotime("-1 month"), " ";      // 距离现在一个月前的时间戳 echo strtotime("+1 year"), " ";       // 距离现在一年后的时间戳 echo strtotime("+1 week 2 days 4 hours 2 seconds"), " "; // 距离现在1周2天4小时2秒后的时间戳 echo strtotime("next Thursday"), " ";   // 下个星期三 echo strtotime("last Monday"), " ";   // 本月的最后一个星期一

    4、mktime()函数

    <?php
    //任何给定月份的最后一天都可以被表示为下个月的第 "0" 天,而不是 -1 天
    //参数位置分别代表 时,分,秒,月,天,年
    $lastday = mktime(0, 0, 0, 3, 0, 2000); echo strftime("Last day in Feb 2000 is: %d", $lastday); $lastday = mktime(0, 0, 0, 4, -31, 2000); echo strftime("Last day in Feb 2000 is: %d", $lastday); ?>
  • 相关阅读:
    Add Binary <leetcode>
    那些坑
    面试集锦
    随看随记
    View的事件处理流程
    android studio view.setId报错
    EditText的hint不显示
    EditText 焦点
    Android拍照的那些事
    微信支付提示签名错误
  • 原文地址:https://www.cnblogs.com/iAmSoScArEd/p/10887925.html
Copyright © 2011-2022 走看看