本文實例講述了PHP簡單獲取上月、本月、近15天、近30天方法。分享給大家供大家參考,具體如下:
/** * 獲取統(tǒng)計時間 * @param $type * 1 上月 * 2 本月 * 3 近15天 * 4 近30天 * @return array */ function getDateInfo($type) { $data = array( array( 'firstday' => date('Ym01', strtotime('-1 month')), 'lastday' => date('Ymt', strtotime('-1 month')), ), array( 'firstday' => date('Ym01', strtotime(date("Y-m-d"))), 'lastday' => date('Ymd', strtotime((date('Ym01', strtotime(date("Y-m-d")))) . " +1 month -1 day")), ), array( 'firstday' => date('Ymd', strtotime("-15 day")), 'lastday' => date('Ymd', strtotime('-1 day')), ), array( 'firstday' => date('Ymd', strtotime("-30 day")), 'lastday' => date('Ymd', strtotime('-1 day')), ), ); return is_null($type) ? $data : $data[$type-1]; } print_r(getDateInfo(1));//獲取上個月第一天與最后一天
運行結(jié)果:
Array ( [firstday] => 20170601 [lastday] => 20170630 )
PS:這里再為大家推薦幾款時間及日期相關(guān)工具供大家參考使用:
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計算器/相差天數(shù)計算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數(shù)差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php日期與時間用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
【網(wǎng)站聲明】本站除付費源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學習交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。