Grab all Wednesdays in a given month in PHP

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

    Use this function to get any days total count in a month

        function getTotalDays($year, $month, $day){
            $from = $year."-".$month."-01";
            $t=date("t",strtotime($from));
    
            for($i=1; $i<$t; $i++){
               if( strtolower(date("l",strtotime($year."-".$month."-".$i)))== $day){
                 $count++;
              }
          }
          return $count;
     }
    

    use like this getTotalDays('2014', '08','monday');

    will output 4

    But if you want all dates in a month on a particular day then use this function

        function getTotalDatesArray($year, $month, $day){
            $date_ar=array();
            $from = $year."-".$month."-01";
            $t=date("t",strtotime($from));
    
            for($i=1; $i<$t; $i++){
               if( strtolower(date("l",strtotime($year."-".$month."-".$i)))== $day){
                $j= $i>9 ? $i: "0".$i;
                 $date_ar[]=$year."-".$month."-".$j;
              }
          }
          return $date_ar;
     }
    

    use like this getTotalDatesArray('2014', '08','monday');

    will output Array ( [0] => 2014-08-04 [1] => 2014-08-11 [2] => 2014-08-18 [3] => 2014-08-25 )

提交回复
热议问题