Home  >  Article  >  Backend Development  >  [php smart class schedule] Select the day of the week in a certain date range and get the date corresponding to the day of the week

[php smart class schedule] Select the day of the week in a certain date range and get the date corresponding to the day of the week

little bottle
little bottleforward
2019-04-20 15:58:572475browse

I recently used this in a curriculum project. There is a date range. After selecting the day of the week, I want to get the date corresponding to the selected week in this date range, as shown below, and the code is as follows


function getDateByWeek($data)
{    $start_date = strtotime($data['start_date']);    $end_date = strtotime($data['end_date']);    $days = ($end_date - $start_date) / 86400;    $weekArr = array('周日','周一','周二','周三','周四','周五','周六');    $newDate = array();    // 组建数组格式 $dataWeek['日期'] => 星期
    for ($i=0; $i < $days; $i++) { 
        $num_week = date('w',$start_date+($i*86400));        $dateWeek[date('Y-m-d',$start_date+($i*86400))] = $weekArr[$num_week];
    }    // 查找两个数组的交集,即获取提交的星期对应的日期
    $newDate=array_intersect($dateWeek,$data['items']);    // 获取数组中的键值(日期),并组成一个新数组
    $date = array_keys($newDate);    return $date;
}

Related tutorials: PHP video tutorial

The above is the detailed content of [php smart class schedule] Select the day of the week in a certain date range and get the date corresponding to the day of the week. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete