【精简】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();
嗯~现在拿到了start和end 时间戳,你可为所欲为了~!!!
上一篇 下一篇

猜你喜欢

热点阅读