大家在做开发时经常需要统计本月、本周的数据,如何获取获取正确获取时间戳范围非常关键,废话不多说,直接上代码!
我本地时间为 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(){
$startDate = date('Y-m-d',strtotime('this week'));//2021-06-21
$endDate = date('Y-m-d',strtotime('next week'));//2021-06-28
$start = strtotime($startDate);
$end = strtotime($endDate);
return [$start,$end];
}
//结果:
Array
(
[0] => 1624204800 //2021-06-21 00:00:00
[1] => 1624809600 //2021-06-28 00:00:00
)
建议使用方式:
list($start,$end)=getCurrMonthEndpointTime();
list($start,$end)=getCurrWeekEndpointTime();