Grab all Wednesdays in a given month in PHP

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

    If DateTime is not given, you need to know the day for one date - start of unix era for example (1st Jan 1970).

    Going from there, you simply locate the first wednesday, and from there find your month. Keep in mind the leap years, and you're good.

    Obviously, it isn't the most efficient algorithm in the world, but it would do the job.

提交回复
热议问题