這篇文章主要介紹了PHP簡單擷取上月、本月、近15天、近30天的方法,結合執行個體形式分析了PHP通過自定義函數封裝的日期與時間戳轉換相關運算技巧,需要的朋友可以參考下
具體如下:
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));//擷取上個月第一天與最後一天
運作結果:
Array
(
[firstday] => 20170601
[lastday] => 20170630
)
相關推薦: