获得自然日周月时间

1.以下函数都可以直接使用。
function getReportDates($periods){
$dates = array();
//当前时间周期的起止时间
$end = getWeekendByDate($periods);
$start = date("Y-m-d",strtotime("-".($periods-1). "day",strtotime($end)));
//获取日期($times个周期)
$dates['start'] = $start." 00:00:00";
$dates['end'] = $end." 23:59:59";
return $dates;
}

/**
* 获取自然 天,周,月
* @param date:今天的日期
* @return array:开始时间和结束时间.
*/
function getWeekendByDate($periods){ //$periods是要的天数 1 7 30

$st = array(1=>'day',7=>'week',30=>'month');

if(array_key_exists($periods,$st)){
        $type = $st[$periods];
}else{
$type = '';
}

switch($type){
case 'week':
$day = date('N', time()); //当前日期是星期几
if($day<2){
return date("Y-m-d", time() - 86400 * ($day+7));
}else {//得到上一周的周末日期。
return date("Y-m-d", time() - 86400 * $day);
}
case 'day':
return date("Y-m-d", time() - 86400 * 2); // 返回前天的日期
case 'month':
if((int)date("d",time())<2){
return date("Y-m-d",strtotime(date("Y-m-0",strtotime("-1 month",strtotime(date("Y-m",time()))))));
}else{
return date("Y-m-d",strtotime(date("Y-m-0",time())));
}
default:
return date("Y-m-d",strtotime("-1 day")); //每天的前一天
}
}
原文地址:https://www.cnblogs.com/kobigood/p/4148315.html