《PHP編程:PHP簡單獲取上月、本月、近15天、近30天的方法示例》要點:
本文介紹了PHP編程:PHP簡單獲取上月、本月、近15天、近30天的方法示例,希望對您有用。如果有疑問,可以聯系我們。
PHP學習本文實例講述了PHP簡單獲取上月、本月、近15天、近30天方法.分享給大家供大家參考,具體如下:
PHP學習
/**
* 獲取統計時間
* @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));//獲取上個月第一天與最后一天
PHP學習運行結果:
PHP學習
Array
(
[firstday] => 20170601
[lastday] => 20170630
)
PHP學習PS:這里再為大家推薦幾款時間及日期相關工具供大家參考使用:
PHP學習在線日期/天數計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
PHP學習在線日期計算器/相差天數計算器:
http://tools.jb51.net/jisuanqi/datecalc
PHP學習在線日期天數差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
PHP學習Unix時間戳(timestamp)轉換工具:
http://tools.jb51.net/code/unixtime
PHP學習更多關于PHP相關內容感興趣的讀者可查看本站專題:《php日期與時間用法總結》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP學習希望本文所述對大家PHP程序設計有所幫助.