php获取 本月 本周 或者 下月 下周的 开始时间 结束时间

时间:2022-09-16 06:25:10
<?php
$now_time = time();
$date=date("Y-m-d",$now_time);
function get_date($date,$t='d',$n=0)
{
if($t=='d'){
$firstday = date('Y-m-d 00:00:00',strtotime("$n day"));
$lastday = date("Y-m-d 23:59:59",strtotime("$n day"));
}elseif($t=='w'){
if($n!=0){$date = date('Y-m-d',strtotime("$n week"));}
$lastday = date("Y-m-d 00:00:00",strtotime("$date Sunday"));
$firstday = date("Y-m-d 23:59:59",strtotime("$lastday -6 days"));
}elseif($t=='m'){
if($n!=0){$date = date('Y-m-d',strtotime("$n months"));}
$firstday = date("Y-m-01 00:00:00",strtotime($date));
$lastday = date("Y-m-d 23:59:59",strtotime("$firstday +1 month -1 day"));
}
return array($firstday,$lastday);
}
$day1 = get_date($date,'d');
$day2 = get_date($date,'d',-1);
$week1 = get_date($date,'w');
$week2 = get_date($date,'w',-1);
$month1 = get_date($date,'m');
$month2 = get_date($date,'m',-1);
echo '<pre>';
print_r($day1);//今天
print_r($day2);//昨天
print_r($week1);//这周
print_r($week2);//上周
print_r($month1);//这月
print_r($month2);//上月
echo '</pre>';