Home >Backend Development >PHP Tutorial >PHP Gets the Date Range of the Current Week (Month)_PHP Tutorial
The program needs to write a method to get the date range of the current week, that is, the date range from Monday to Sunday.
Function getWeekRange($date){
$ret=array();
$timestamp=strtotime($date);
$w=strftime('%u',$timestamp);
$ret['sdate']=date('Y-m-d 00:00:00',$timestamp-($w-1)*86400);
$ret['edate']=date('Y-m-d 23:59:59',$timestamp+(7-$w)*86400);
return $ret;
}
//author:zhxia Get the start date and end date of the month where the specified date is located
Function getMonthRange($date){
$ret=array();
$timestamp=strtotime($date);
$mdays=date('t',$timestamp);
$ret['sdate']=date('Y-m-1 00:00:00',$timestamp);
$ret['edate']=date('Y-m-'.$mdays.' 23:59:59',$timestamp);
return $ret;
}
//author:zhxia Application of the above two functions
Function getFilter($n){
$ret=array();
switch($n){
Case 1:// Yesterday
$ret['sdate']=date('Y-m-d 00:00:00',strtotime('-1 day'));
$ret['edate']=date('Y-m-d 23:59:59',strtotime('-1 day'));
break;
Case 2://This week
$ret=getWeekRange(date('Y-m-d'));
break;
case 3://last week
$strDate=date('Y-m-d',strtotime('-1 week'));
$ret=getWeekRange($strDate);
break;
Case 4: //Last week
$strDate=date('Y-m-d',strtotime('-2 week'));
$ret=getWeekRange($strDate);
break;
Case 5: //This month
$ret=getMonthRange(date('Y-m-d'));
break;
Case 6://Last month
$strDate=date('Y-m-d',strtotime('-1 month'));
$ret=getMonthRange($strDate);
break;
}
return $ret;
}