Grab all Wednesdays in a given month in PHP

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

    There's probably a more efficient way to do it, but this works:

    You can test with this:

    foreach (getWednesdays($argv[1], $argv[2]) as $date) {
        echo date("Y-m-d\n", $date);
    }
    
    $ php wednesdays.php 11 2010
    2010-11-03
    2010-11-10
    2010-11-17
    2010-11-24
    $ php wednesdays.php 12 2010
    2010-12-01
    2010-12-08
    2010-12-15
    2010-12-22
    2010-12-29
    $ php wednesdays.php 2 2012
    2012-02-01
    2012-02-08
    2012-02-15
    2012-02-22
    2012-02-29
    

    S

提交回复
热议问题