Grab all Wednesdays in a given month in PHP

前端 未结 11 1681
长发绾君心
长发绾君心 2020-12-09 18:36

This is the function I\'m trying to write:

function getWednesdays($month, $year) {
   // Returns an array of DateTimes representing all Wednesdays this month         


        
11条回答
  •  既然无缘
    2020-12-09 19:20

    I have re-written @Gordon's solution to suit for any day

    function get_dates_of_day($y, $m, $day_name)
    {
        return new DatePeriod(
            new DateTime("first $day_name of $y-$m"),
            DateInterval::createFromDateString("next $day_name"),
            new DateTime("next month $y-$m-01")
            );
    }
    

    Now you can simply do get_dates_of_day('2015','08','wednesday') and use the same to get dates for any day in a given month.

提交回复
热议问题