本文主要是介绍php用户选择几月第几个星期几获得具体日期,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
/**
* @param $year_month 需要计算的年月
* @param $how_many 第几个日期
* @param $what_week 选择周几
* @return array|string[]
*/
public function generation($year_month,$how_many,$what_week)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
/*$year_month = "2021-07";
$how_many = '3'; //选择第几个 1.第一个····5第五个、6最后一个
//1周一,2周二,3周三,4周四,5周五,6周六,7周日,8自然日,9工作日,10周末
$what_week = '9'; //选择周几
*/
if ($what_week < 7){//周一-周六
$what_week = $what_week;
$week_info = $this->get_week($how_many,$what_week,$year_month);
$data =array(
'start_time' => $week_info,
'end_time' =>$week_info
);
return $data;
}elseif ($what_week == 7){//周日
$what_week = 0;
$week_info = $this->get_week($how_many,$what_week,$year_month);
$data =array(
'start_time' => $week_info,
'end_time' =>$week_info
);
return $data;
}elseif ($what_week == 8){//返回当月一号
if ($how_many <= 5){
$week_info = $year_month.'-0'.$how_many;
}else{
$week_info = date('Y-m-d', strtotime(date('Y-m-01', strtotime($year_month.'-01')) . ' 1 month -1 day'));
}
$data =array(
'start_time' => $week_info,
'end_time' =>$week_info
);
return $data;
}elseif ($what_week == 9){//工作日计算
$start_time = $year_month.'-01'; //當月第一天
$end_time = date('Y-m-d', strtotime(date('Y-m-01', strtotime($year_month.'-01')) . ' 1 month -1 day'));
$info = $this->get_work_day($start_time,$end_time);//获取当月所有的工作日
$workday = $info['workday'];
if ($how_many <= 5){ //输入的第几周大于实际的周数,默认使用最后一周
$week_info = $workday[$how_many-1];
}else{
$work_num = count($workday);
$week_info = $workday[$work_num-1];
}
$data =array(
'start_time' => $week_info,
'end_time' =>$week_info
);
return $data;
}elseif ($what_week == 10){ //获取周末
//周六
$start_time = $this->get_week($how_many,6,$year_month);
$start = explode('-',$start_time);
//周日
$end_time = $this->get_week($how_many,0,$year_month);
$end = explode('-',$end_time);
if ($start[2] >$end[2]){
$day = $end[2]-1;
$end_time = $end_time;
$start_time = $end[0].'-'.$end[1].'-'.$day;
}
$data =array(
'start_time' => $start_time,
'end_time' =>$end_time
);
return $data;
}
}
/**
* @param $what_week
* @param $week
* @param $year_month
* @return mixed 计算第几个周几的日期
*/
public function get_week($week,$what_week,$year_month){
if(empty($year_month)){
$year_month = date("Y-m");
}
$maxDay = date('t', strtotime($year_month."-01"));
//计算出当前月份所有周几
$mondays = array();
for($i=1; $i<=$maxDay; $i++){
if(date('w', strtotime($year_month."-".$i)) == $what_week){
$mondays[] = $year_month."-".($i>9?'':'0').$i;
}
}
//获取当月有几个周几
if ($week >= count($mondays)){ //输入的第几周大于实际的周数,默认使用最后一周
$week_num = count($mondays);
$week_info = $mondays[$week_num-1];
}else{
$week_info = $mondays[$week-1];
}
return $week_info;
}
/**
* @param $start_time 月份第一天
* @param $end_time 月份最后一天
* @return array|array[] 每个月所有的工作日和周六周日日期
*/
function get_work_day($start_time,$end_time)
{
$start_time = empty($start_time)?time():strtotime($start_time);
$end_time = empty($end_time)?time():strtotime($end_time)+86400;//最后一天也算
if($end_time<=$start_time){
return array();
}
$days = ($end_time - $start_time)/86400;
$work_day_array = array();//工作日
$Weekend_day_array = array();//周末
for ($i=0; $i < $days ; $i++) {
$timer = $start_time + 86400*$i;
$num= date("N",$timer);
if($num == 6 || $num == 7) {
$Weekend_day_array[] = date('Y-m-d',$timer);
}else{
$work_day_array[] = date('Y-m-d',$timer);
}
}
return array(
'workday'=>$work_day_array, //所有工作日
//'weekend'=>$Weekend_day_array //所有周末
);
}
这篇关于php用户选择几月第几个星期几获得具体日期的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!