Find every Sunday between two dates

后端 未结 4 1298
无人及你
无人及你 2020-12-10 09:08

I have $fromdate and $todate variables. I want a function that can calculate the dates of each Sunday existing in between $fromdate an

4条回答
  •  一整个雨季
    2020-12-10 09:28

    Try this

    function getDateForSpecificDayBetweenDates($start, $end, $weekday = 0){
    
    $weekdays="Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday";
    
    $arr_weekdays=split(",", $weekdays);
    $weekday = $arr_weekdays[$weekday];
    if(!$weekday)
        die("Invalid Weekday!");
    
    $start= strtotime("+0 day", strtotime($start) );
    $end= strtotime($end);
    
    $dateArr = array();
    $friday = strtotime($weekday, $start);
    while($friday <= $end)
    {
        $dateArr[] = date("Y-m-d", $friday);
        $friday = strtotime("+1 weeks", $friday);
    }
    $dateArr[] = date("Y-m-d", $friday);
    
    return $dateArr;
    }
    

    Here's how this function can be called...

    $dateArr = getDateForSpecificDayBetweenDates("Today", "+1 year", 0); // 0 Sun, 1 Mon, etc.
    

    $start and $end dates will take anything that strtotime() supports.

提交回复
热议问题