php中時間函數date及常用的時間計算圖文詳解

來源:互聯網
上載者:User
本篇文章主要介紹了php中時間函數date及常用的時間計算的相關知識,具有很好的參考價值。下面跟著小編一起來看下吧

曾在項目中需要使用到今天,昨天,本周,本月,本季度,今年,上周上月,上季度等等時間戳記,趁最近時間比較充足,因此計劃對php的相關時間知識點進行總結學習

1,閱讀php手冊date函數

常用時間函數:

    checkdate()驗證一個時間是否正確

    date_default_timezone_get()取得當前指令碼所使用的時區

    date_default_timezone_set()設定指令碼所用時區 ini_set()也可以滿足,或者修改設定檔

    date_sunrise() date_sunset() 返回給定的日期和地點的日出時間和日落時間

    date()格式化一個日期,下邊會有詳細內容

    getdate() 取得日期時間的相關資訊

    gettimeofday()取得目前時間的相關資訊

    idate()將本地時間日期格式化為整數,但只接受一個字元作為參數

    microtime()返回當前的時間戳記和秒數

    mktime()取得一個日期的時間戳記

    strtotime()將英文文本的日期秒數解析為時間戳記

2,重要函數詳解

date()格式化一個日期

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

    d    月份中的第幾天,也就是幾號,此為具有前置字元為零,例如01,02

    D    星期中的第幾天,也就是英文星期幾的單詞縮寫,Mon到Sun

    l(L小寫) 星期幾,此為完整的英文格式, Sunday到Saturday

    N    用數字表示星期幾,1為星期一,7為星期日

    S    每月天數後面的英文尾碼

    w    星期中的第幾天,使用數字表示,0為星期天,6為星期六

    z    年份中的第幾天 0到365

    W    年份中的第幾周

    F    月份,完整的英文單詞

    m    月份數字格式,具有前置0

    M    三個字母表示的縮寫的月份

    n    數字表示的月份,沒有前置0

    t    給定月份所應有的天數

    L    檢測是否為閏年,閏年為1,月份為0

    Y    4位元字表示的年份

    y    2位元字表示的年份

    a    小寫上午或者下午的值

    A    大寫的上午或者下午的值

    g    12小時制,沒有前置0

    G    24小時制,沒有前置0

    h    12小時制,有前置0

    H    24小時制,有前置0

    i    具有前置0的分鐘數

    s    秒數,具有前置0

    u    毫秒,date()函數返回的是000000格式的

    e    時區標識

    I    是否為夏令時,是為1,不是為0

    T    本機所在的時區

    c    2017-05-08T 15:22:21+00:00 格式的時間

    U    從1970開始至今的秒數

idate()函數詳解

與date的區別是此函數只可以傳遞一個參數,date()可以傳遞多個參數

    B    Internet time

    d    月份中的第幾天

    h    12小時制的時間

    H    24小時制的時間

    i    分鐘

    I    若啟用夏令時返回1,否則為0

    L    如果是閏年則返回1,否則返回0

    m    月份的數字

    s    秒數

    t    本月的總天數

    U    從1970起的秒數

    w    星期中的第幾天

    W    年份中的第幾個星期,星期從星期一開始

    y    年份,1或者2位元字

    Y    年份4位元字

    z    年份中的第幾天

    Z    以秒為單位的時區位移量

strtotime()函數銜接

用法樣本

      strtotime ("now");      strtotime ("10 September 2017");      strtotime ("+1 day");      strtotime ("+1 week");      strtotime ("+1 week 2 days 4 hours 2 seconds");      strtotime ("next Thursday");      strtotime ("last Monday");

3,常用時間匯總

$times = [];function makeTime(){  //擷取今日開始時間戳和結束時間戳記  $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));  $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;  $times['today']['begin'] = $beginToday;  $times['today']['end'] = $endToday;  //擷取昨日起始時間戳記和結束時間戳記  $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));  $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;  $times['yesterday']['begin'] = $beginYesterday;  $times['yesterday']['end'] = $endYesterday;  //擷取上周起始時間戳記和結束時間戳記  $beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));  $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));  $times['lastWeek']['begin'] = $beginLastweek;  $times['lastWeek']['end'] = $endLastweek;  //擷取本月起始時間戳記和結束時間戳記  $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));  $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));  $times['thisMonth']['begin'] = $beginThismonth;  $times['thisMonth']['end'] = $endThismonth;  //擷取本周開始時間和結束時間,此例中開始時間為周一  $defaultDate = date('Y-m-d');  $first = 1;  $w = date('w',strtotime($defaultDate));  $beginWeek = strtotime("$defaultDate-" . ($w?$w-$first:6) . 'days');  $endWeek = $beginWeek + 6*24*3600-1;  $times['thisWeek']['begin'] = $beginWeek;  $times['thisWeek']['end'] = $endWeek;  //擷取上月的起始時間戳記和結束時間戳記  $beginLastmonth=mktime(0,0,0,date('m')-1,1,date('Y'));  $endLastmonth=mktime(23,59,59,date('m')-1,date('t'),date('Y'));  $times['LastMonth']['begin'] = $beginLastmonth;  $times['LastMonth']['end'] = $endLastmonth;  //擷取今年的起始時間和結束時間  $beginThisyear = mktime(0,0,0,1,1,date('Y'));  $endThisyear = mktime(23,59,59,12,31,date('Y'));  $times['thisYear']['begin'] = $beginThisyear;  $times['thisYear']['end'] = $endThisyear;  //擷取上年的起始時間和結束時間  $beginLastyear = mktime(0,0,0,1,1,date('Y')-1);  $endLastyear = mktime(23,59,59,12,31,date('Y')-1);  $times['lastYear']['begin'] = $beginLastyear;  $times['lastYear']['end'] = $endLastyear;  //擷取本季度開始時間和結束時間  $season = ceil((date('n'))/3);//當月是第幾季度  $beginThisSeason = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));  $endThisSeason = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));  $times['thisSeason']['begin'] = $beginThisSeason;  $times['thisSeason']['end'] = $endThisSeason;  //擷取上季度的起始時間和結束時間  $beginLastSeason = mktime(0, 0, 0,($season-1)*3-3+1,1,date('Y'));  $endLastSeason = mktime(23,59,59,($season-1)*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));  $times['lastSeason']['begin'] = $beginLastSeason;  $times['lastSeason']['end'] = $endLastSeason;  return $times;}$times = makeTime();

目前是我之前用到的時間戳記,後期還會積累匯總,避免重複造輪子。

相關推薦:

php date函數介紹與使用方法詳解

關於PHP MySQL Update語句的相關內容

PHP驗證碼類ValidateCode

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.