【精简】PHP 获取本月第一天、最后一天,本周第一天、最后一天时
2019-08-14 本文已影响0人
夜空中乄最亮的星
大家在做开发时经常需要统计本月、本周的数据,如何获取获取正确获取时间戳范围非常关键,废话不多说,直接上代码!
我本地时间为 2019年8月14日
获取本月端点时间戳
function getCurrMonthEndpointTime(){
$start=strtotime(date('Y-m'));
$end=strtotime(date('Y-m',strtotime('+1 month')))-1;
return [$start,$end];
}
//结果:
Array
(
[0] => 1564588800 //2019-08-01 00:00:00
[1] => 1567267199 // 2019-08-31 23:59:59
)
获取本周端点时间戳
function getCurrWeekEndpointTime(){
$nowWeek=date('N');
$startDate=1==$nowWeek?date('Y-m-d'):date('Y-m-d',strtotime('previous monday'));
$start=strtotime($startDate);
$formate='Y-m-d 23:59:59';
$endDate=7==$nowWeek?date($formate):date($formate,strtotime('next sunday'));
$end=strtotime($endDate);
return [$start,$end];
}
//结果:
Array
(
[0] => 1565539200 //2019-08-12 00:00:00
[1] => 1566143999//2019-08-18 23:59:59
)
建议使用方式:
list($start,$end)=getCurrMonthEndpointTime();
list($start,$end)=getCurrWeekEndpointTime();