Grab all Wednesdays in a given month in PHP

前端 未结 11 1703
长发绾君心
长发绾君心 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:18

    Try This one to get all Wednesdays in current month.

    $month  = date('m');
    $year  = date('Y');
    function getDays($year,$month){ 
     $days = cal_days_in_month(CAL_GREGORIAN, $month,$year);
     $wed = array();
     for($i = 1; $i<= $days; $i++){
     $day  = date('Y-m-'.$i);
     $result = date("D", strtotime($day));
     if($result == "Wed"){  
     $wed[] = date("Y-m-d", strtotime($day)). " ".$result."
    "; } } return $wed; } $wed = getDays($year,$month); print_r($wed);

提交回复
热议问题